Search Unity

Resolved TextMeshPro text recoloring issue

Discussion in 'UGUI & TextMesh Pro' started by samboknight, Jan 25, 2023.

  1. samboknight

    samboknight

    Joined:
    Aug 31, 2019
    Posts:
    25
    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:

    Peek 2023-01-25 11-25.gif
    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.
     

    Attached Files:

  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    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.
     
  3. samboknight

    samboknight

    Joined:
    Aug 31, 2019
    Posts:
    25
    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.
     
    Last edited: Jan 26, 2023
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    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?
     
  5. samboknight

    samboknight

    Joined:
    Aug 31, 2019
    Posts:
    25
    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.
     
  6. samboknight

    samboknight

    Joined:
    Aug 31, 2019
    Posts:
    25
    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:

    Code (CSharp):
    1. public static void ChangeTextColor(TMP_Text tmp, int firstCharacterIndex, int characterCount, Color32 color)
    2.         {
    3.             for (int i = 0; i < characterCount; ++i)
    4.             {
    5.                 int charIndex = firstCharacterIndex + i;
    6.  
    7.                 // fix for first character changing color
    8.                 // spaces have no geometry, which apparently just results in grabbing the wrong vertices
    9.                 if (tmp.textInfo.characterInfo[charIndex].character == ' ')
    10.                     continue;
    11.                  
    12.                 int meshIndex = tmp.textInfo.characterInfo[charIndex].materialReferenceIndex;
    13.                 int vertexIndex = tmp.textInfo.characterInfo[charIndex].vertexIndex;
    14.  
    15.                 Color32[] vertexColors = tmp.textInfo.meshInfo[meshIndex].colors32;
    16.                 vertexColors[vertexIndex + 0] = color;
    17.                 vertexColors[vertexIndex + 1] = color;
    18.                 vertexColors[vertexIndex + 2] = color;
    19.                 vertexColors[vertexIndex + 3] = color;
    20.             }
    21.             tmp.UpdateVertexData(TMP_VertexDataUpdateFlags.All);
    22.         }
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    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?
     
  8. samboknight

    samboknight

    Joined:
    Aug 31, 2019
    Posts:
    25
    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.
     
    Kurt-Dekker likes this.