How to identify the max character capacity for a Text in canvas?

Hi!

I am trying to create something like a sort of a dialog box where I can show word-by-word of a string with a delay. But when the text inside the text box exceeds the maximum character capacity and can not display the new word anymore, I want to clean up that text box. The problem is that I don’t know how to compare and identify which is the maximum character capacity inside the Text box.

What I’ve accomplished so far is just that:

    public string sampleText; // Text to be displayed in the dialog box
    public Text sampleTextBox; // dialog box inside canvas
    public float textDelay; // delay between word display

    public IEnumerator GenerateText()
    {
        string[] sampleTextArray = sampleText.Split(); ; // Array that splits the string, separeting by the spaces.
        sampleTextBox.text = ""; // Cleanning up the dialog box.

        foreach (string s in sampleTextArray) //For each word in the array of words from string to be displayed
        {
            // IF sampleTextBox.text + WHAT I WANT TO DISPLAY <= MAX CHARACTER CAPACITY IN DIALOG BOX
                // sampleTextBox.text += WHAT I WANT TO DISPLAY
            // ELSE
                // sampleTextBox.text = ""; // Cleanning up the dialog box.
                // sampleTextBox.text += WHAT I WANT TO DISPLAY

            // sampleTextBox.text += " " // ADDING SPACE BETWEEN WORDS
        }
    }

Thanks for reading this far!

If you want any clarification to answer my question, you can ask.

Hi,

Using a monospaced font should make it easier to estimate the width of the text by counting letters. Assuming you know the letter width, and the width of the text box.

Not sure though how it may affect the readability.

1 Like

Not sure about the old Text object but the TextMeshPRO package can give you information on how big pieces of rendered text are getting and let you make decisions like what you want.

Look for discussions such as:

https://discussions.unity.com/t/694589

1 Like