I have a system set up that is supposed to change the color of links when the cursor hovers over them. It mostly works, but for some reason, whenever it changes the color of a link, it also changes the color of the first character in the text mesh:

I have been unable to determine why this happens. Can anyone here point me in the right direction? Testing this will require that you have the TextMeshPro Examples & Extras installed. I have included the scene shown in the .gif above and all my relevant scripts in the .zip file. Unity version is 2021.3.14.
8756704–1186735–TMP Bug.zip (7.27 KB)
Looks like you might be changing the font object instead of changing the TextMesh-whatever rendering object?
Either way, plenty of docs on changing text color in TMPro, go there and make sure that’s how you’re doing it.
If I were changing the font object, would it not apply to all the characters, since they are all in the same font?
I’m just changing the vertex colors like what is done in the TMP_TextSelector_B example script, as far as I can tell, and that script doesn’t have this issue.
Are all your links under one textmeshpro object? If so, is there a reason you are doing it that way instead of having them in separate objects?
Of course they’re under one textmeshpro object. That’s what’s done in all the examples, and it’s what’s reasonable to do. The example in my first post was a simple demonstration of the problem. In my game, there are multiple paragraphs of text, each containing several inline links. What you are suggesting would be extremely tedious and time-consuming, and it is not a reasonable solution.
Apparently the issue was caused by spaces in the link text. From what I gather, spaces have no geometry, which causes vertexIndex to be set to 0. This, in turn, causes the vertices of the first character to be changed. My solution is to skip spaces:
public static void ChangeTextColor(TMP_Text tmp, int firstCharacterIndex, int characterCount, Color32 color)
{
for (int i = 0; i < characterCount; ++i)
{
int charIndex = firstCharacterIndex + i;
// fix for first character changing color
// spaces have no geometry, which apparently just results in grabbing the wrong vertices
if (tmp.textInfo.characterInfo[charIndex].character == ' ')
continue;
int meshIndex = tmp.textInfo.characterInfo[charIndex].materialReferenceIndex;
int vertexIndex = tmp.textInfo.characterInfo[charIndex].vertexIndex;
Color32[] vertexColors = tmp.textInfo.meshInfo[meshIndex].colors32;
vertexColors[vertexIndex + 0] = color;
vertexColors[vertexIndex + 1] = color;
vertexColors[vertexIndex + 2] = color;
vertexColors[vertexIndex + 3] = color;
}
tmp.UpdateVertexData(TMP_VertexDataUpdateFlags.All);
}
Do you mean like a space after "Link X"
, something like "Link X "
?
If so, I wonder, did you just discover a massive bug in TMPro??!
Or am I misunderstanding something?
A space anywhere in the link text (meaning the text between pairs of link tags) was causing the issue. "Link X"
and "Link X "
would both cause an issue. It does look like a bug, though I wouldn’t consider it massive. This didn’t come up in the examples because they changed the color of words, which will never have spaces in them (because putting a space in the middle of a word makes it into two words). However, link tags can have spaces inside them.
1 Like