IsTextTruncated not working as exptected even though forcing meshUpdate

I am trying to fill a two lined dialogue box with a sentence. If that sentence were to be truncated, i.e. is too long for the dialogue box to display, I want to store the cut off words separately and display them in the next dialogue box.

To achieve that, I implement a do while loop adding one word at a time to the TextMeshProUGUI and checking if the current text is truncated or not. Unfortunately isTextTruncated always returns true in my case, thus exiting the do while loop after the first iteration. I am using FoceMeshUpdate as well as Canvas.ForceUpdateCanvases() to be safe, but it still doesn’t seem to work.

public void ParseWords(Dialog dialogue, int block)
    {
        Debug.Log("Starting conversation with " + dialogue.Name);

        //Words.Clear();
        Sentences.Clear();

        string text = dialogue.SentenceBlock[block]; //text block to be separted into different sentences
        var words = text.Split(new char[] { ' ' }, System.StringSplitOptions.RemoveEmptyEntries); //text block parsed into words

        int counter = 0;

        while(counter < words.Length)
        {
            DialogueText.text = "";
            string sentence = "";
            Debug.Log("is text truncated at beginning ?" + DialogueText.isTextTruncated); // is true
            do {
                if (counter >= words.Length) break;
                sentence += words[counter] + " ";
                DialogueText.text = sentence;
                DialogueText.ForceMeshUpdate();
                counter++;
                Canvas.ForceUpdateCanvases();
            } while (!DialogueText.isTextTruncated) ; // returns false, thus exiting the do while loop

            Sentences.Enqueue(sentence);
            Debug.Log(sentence);
        }

        // DisplayNextSentence();
    }

One more thing, if I check isTextTruncated on awake on a random TextMeshProUGUI.text, it returns the expected bool, meaning that it does seem to work normally, but not in my loop.

Why not use the Page overflow mode? It is designed specifically to push the text that doesn’t fit to the next page for things like dialogues.

That would be better than doing string concatenation.

@Stephan_B I can’t believe I didn’t catch that earlier. Thanks a lot for the help. Page overflow works exactly as expected.

1 Like

@Stephan_B Quick follow up question: Is there any way to get the word count per page? I’d like to animate the words one by one, every time a new page is opened. Looping through all the words from the very beginning would mean a delay in the words displayed in later pages…any ideas?

The TMP_PageInfo only contains the index of the first and last character of the page. It does not contain a word count or index of the first and last word of the page.

As such, when trying to animate the words of a given page, you would have to iterate over each word to check if their first character index is >= the page’s first character index.

Adding the above logic should not have any noticeable performance impact.