WordInfo and Punctuation

I have been unable to find any other discussions about this. I’m writing a script that makes words fade in one at a time. So far it works great, but the “firstCharacterIndex” and “lastCharacterIndex” of “WordInfo” doesn’t seem to count any sort of punctuation such as colons, commas, or question marks.

To clarify, if the first “word” is “5:”, it will say the first and last letter of that word is 0 (the 5). Or if the last word is “Yes?” it will say the last word is a three letter word ending at the “s”.

I’ll go ahead and post my code if it’s any help to anyone.

private IEnumerator RollingText()
    {
        displayText.ForceMeshUpdate(true);
        TMP_TextInfo textInfo = displayText.textInfo;
        int wordCount = textInfo.wordCount;
    
        Color32[] newVertexColors;
        Color32 displayColor = displayText.color;

        Color32 fullColor = new Color32(255, 255, 255, 255);

        for (var i = 0; i < wordCount; i++)
        {
            TMP_WordInfo wordInfo = textInfo.wordInfo[i];
            int wordStart = wordInfo.firstCharacterIndex;
            int wordEnd = wordInfo.lastCharacterIndex;

            for (int j = 0; j < 5; j++)
            {
            
                fullColor.a = (byte)(255*(j/5f));
                displayColor = fullColor;
            
                Debug.Log(j);
            
                for (int k = wordStart; k <= wordEnd; k++)
                {
                    int materialIndex = textInfo.characterInfo[k].materialReferenceIndex;
                    newVertexColors = textInfo.meshInfo[materialIndex].colors32;
                    int vertexIndex = textInfo.characterInfo[k].vertexIndex;

                    newVertexColors[vertexIndex + 0] = displayColor;
                    newVertexColors[vertexIndex + 1] = displayColor;
                    newVertexColors[vertexIndex + 2] = displayColor;
                    newVertexColors[vertexIndex + 3] = displayColor;

                    displayText.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
                }

                yield return null;

            }
        
            yield return null;
        }
    }
void ChangeTokenColor(int startIndex, int endIndex, Color32 color)
    {
        //for completed tokens
        for (int index = 0; index < startIndex; index++)
        {
            TMP_WordInfo wInfo = pageTextField.textInfo.wordInfo[index];
            ChangeColor(wInfo, TextColors.completedColor);
        }

        //for flashing and highlighting
        for (int index = startIndex; index < endIndex; index++)
        {
            TMP_WordInfo wInfo = pageTextField.textInfo.wordInfo[index];
            ChangeColor(wInfo, color);
        }

        //for words
        if (startIndex == endIndex)
        {
            TMP_WordInfo wInfo = pageTextField.textInfo.wordInfo[startIndex];
            ChangeColor(wInfo, color);
        }
          
        // Update Geometry
        pageTextField.UpdateVertexData(TMP_VertexDataUpdateFlags.All);
    }

    private void ChangeColor(TMP_WordInfo wordInfo, Color32 color)
    {
        // Iterate through each of the characters of the word.
        for (int i = 0; i <= wordInfo.characterCount; i++)
        {
            int characterIndex = wordInfo.firstCharacterIndex + i;

            if (pageTextField.textInfo.characterInfo[characterIndex].character == ' ')
            {
                return;
            }
          
            // Get the index of the material / sub text object used by this character.
            int meshIndex = pageTextField.textInfo.characterInfo[characterIndex].materialReferenceIndex;

            int vertexIndex = pageTextField.textInfo.characterInfo[characterIndex].vertexIndex;

            // Get a reference to the vertex color
            Color32[] vertexColors = pageTextField.textInfo.meshInfo[meshIndex].colors32;

            vertexColors[vertexIndex + 0] = color;
            vertexColors[vertexIndex + 1] = color;
            vertexColors[vertexIndex + 2] = color;
            vertexColors[vertexIndex + 3] = color;
        }
    }

I worked it using this code. I had to ignore the spaces in the text for some buggy coloring in the text. You just had to play the loop <= instead of < to get the last character e.g. Punctuation mark

1 Like