Last letter of line is not correctly detected

Hello,

currently I’m working on a game with a lot of text which I display in a Textbox (TextMeshPro - Text UI). Because my text appears letter by letter it results in shifting a word in a new line if it has too many letters and I don’t like that effect. Therefore I try to check if a word gets truncated and if so, it gets moved by \n. Here the code:

// A segment is the full text of a text box. textSegments is the entire dialogue
        foreach (string segment in textSegments)
        {
            string[] words = segment.Split();
            string oldTextBoxText = TextBox.text;
            foreach (string word in words)
            {
                TextBox.text += word;
                TextBox.ForceMeshUpdate();
                if ( TextBox.isTextTruncated ||
                     TextBox.textInfo.lineInfo[TextBox.textInfo.lineCount - 1].lastCharacterIndex
                    != TextBox.textInfo.lineInfo[TextBox.textInfo.lineCount - 1].lastVisibleCharacterIndex)
                {
                    // Correct the overflow by "reset" the text to the old state
                    TextBox.text = $"{oldTextBoxText}\n{word}";
                }
                else
                    TextBox.text += " ";
                // Update the older textbox text to revert a word if its overflowing.
                oldTextBoxText = TextBox.text;
            }
        }

This places the text word after word and checks if a letter is not visible (with IsTextTruncated and the lastVisibleCharacterIndex).

The problem here is, that if only one letter is missing it doesn’t detect that. Neither of the two recognize that.7592626--941611--TextBox.PNG

Note: I wrote “somehow” seperated on purpose to demonstrate the problem. If I write it normally it works.
I tried also using a different Font Asset but it didn’t work. I’m working with the Version 3.0.6 of TextMeshPro and Unity 2020.3.21f1.

Do I miss something here?

I would suggest using the .maxVisibleCharacter property which controls the number of visible characters while still doing a full layout pass on the whole text thus avoiding the issue you are trying to address while at the same time avoiding using string concatenation which result in GC.

See example 17 - Old Computer Terminal and related scripts which makes use of that .maxVisibleCharacter. This example is included in the TMP Examples & Extras.

Ok I’ve fixed it by adding an extra “W” after each word (which gets deleted afterwards). I also noticed a mistake where text parts which should be invisible would be considered in the detection. Because of that, the text sometimes got cut on the wrong word. It seems to work fine now. I’ll still look in the example to optimize it. Thank you