Font Size in Auto Sized TMP Text "delayed"

Hi,
I have a question regarding Auto Size in TMPText.
I have two buttons, each displaying text that changes at run time. My goal is to go through the buttons, pick the smallest font size, and set this font size to all buttons so that they have uniform font sizes.
However, determining the font size proves difficult. The Font Size of the relevant TMP Text Components return a - from my perspective - not yet updated value.

To Debug, i set a script with this text on one of my buttons:

    private void OnEnable()
    {
        GetComponentInChildren<TMPro.TextMeshProUGUI>().text = "a really really long text text text text text text text text texttexttext";
        float size = GetComponentInChildren<TMPro.TextMeshProUGUI>().fontSize;
        Debug.Log(size);
    }

I then toggle the button object manually and this is what happens:


The Font Size is properly set to 90.35 (text fits its button), but its not what is returned in my Debug-Code.

When I toggle the button again:
7602043--943552--tmp2.png
The value I expected is returned.

At what time in Unity’s order of method calls is “Font Size” applied?
Is there even such a thing?
I can put this Debug-Code in an Update method and at some point the change is made - but I am unable to pinpoint when exactly that happens, and so I do not know when to compare the different font sizes.

Insights into what is going on here would be much appreciated.

Whenever a property of the text object has been changed, the text object will be processed. This processing occurs just before culling.

In the example you provided above, since the text object hasn’t been processed yet when you check the size, it would return whatever the initial size is. However, if you call ForceMeshUpdate() on the text object, this will force the text object to be processed right away where subsequently checking the size would return the correct and updated size.

I would recommend caching the reference to the text object as calling GetComponentInChildren is pretty expensive.

1 Like