Preferred width/height sometimes 0

I’ve got some TextMeshProUGUI components that I poll the preferred width from in order to resize their containers. Sometimes these TMP components returns 0 for the preferred width, even if they have text assigned, and have reported a different width earlier, without me changing anything of the TMP settings.

Here’s an example of an inspector where that’s happening. See the Layout properties at the bottom:

Any idea why this would happen? I tried turning on/off “Visible Descender”, but that didn’t seem to change things.

What version of the TMP package are you using? If not the latest, please be sure to check if the behavior is the same with the latest release.

Does this text object have any layout component affecting it or is this a stand alone text object?

Are you able to reliably reproduce this behavior? If so, can you please provide a simple repro project / scene for me to look at?

1.5.1 for Unity 2018.4.24f1. There’s no layout components anywhere.

I think I found the bug. My code used to do this:

baseSize = textPlateText.GetPreferredValues();

Now it does this:

baseSize = new Vector2(textPlateText.preferredWidth, textPlateText.preferredHeight);

And the bug is gone!

Looking through the TMP code, it seems like GetPreferredValues sets m_isPreferredWidthDirty = false; but it doesn’t actually set m_preferredWidth. So you’ll probably always get this behaviour if you call GetPreferredValues two times in a row.

There’s something really bad going on between GetPreferredWidth, preferredWidth (the getter) and GetPreferredValues.

6369612--708960--upload_2020-10-1_10-16-0.png

I made the necessary changes to address this issue.

The changes will be included in the next release of the TMP package which should be available within the next few days.

1 Like

Hmm I think since that change the TextMeshPro.text is changed when calling GetPreferredValues. Was this an intended change?

Definitely not intended.

I have a potential fix for this that I am testing.

What version of Unity are you using?

Unity 2020 LTS

For testing purposes, replace the TMP_Text.cs file contained in the Global Package Cache\packages\com.unity.textmeshpro@3.0.4\Scripts\Runtime with the attached revised version.

Let me know if that resolves the issue on your end?

6994787–826271–TMP_Text.cs (374 KB)

I tried it, but unfortunately it does not fix the problem. Workaround for now is to store the original text before calling GetPreferredValues

Can you make sure the change was not reverted as this should have resolved that behavior as the text is no longer set by GetPreferredValues(string text) and the other overload also using a string.

Do you mean change when you check the .text property?

How are you setting the text?

Can you provide the steps or simple scene / project that would enable me to reproduce this?

I have a similar issue…but with Height…
currentWidth is 155
Font is 24 point
GetPreferred Value return 200.13 => cellSizer.GetPreferredValues(text, currentWidth, 0f).y

If I set text, then look at cellSizer.preferredHeight I get 232.82 which is the proper height.
Not sure it was broken in 3.0.5 (current version installed), but I didn’t notice it in 3.0.4 here is the code

float currentWidth = cellSizer.rectTransform.rect.width;
cellSize = cellSizer.GetPreferredValues(text, currentWidth, 0f).y;
cellSizer.text = text;
cellSize = cellSizer.preferredHeight;

And the text if you are curious is dsjkfhdklsjflkdjslafjlds;ajfljdsal;kfjlkdsjaflkjdlsakfjlkdasjfl;djsalfjldkasjfl

And the font asset Microsoft OneDrive

1 Like

Let me see if I can reproduce this on my end and follow up without thereafter.

This is what I get in the Editor using the font you provided which is a height of 196.13.

7077472--841951--upload_2021-4-26_1-18-22.png

See if you get the correct values testing with the following simply script.

using UnityEngine;
using TMPro;

public class PreferredValueCheck : MonoBehaviour
{
    public TextMeshProUGUI TextComponentUI;

    private string m_Label_01 = "A few lines of text for testing preferred values.";

    private void Awake()
    {
        Vector2 preferredValues = TextComponentUI.GetPreferredValues(m_Label_01, 155, 0);
        Debug.Log(preferredValues.ToString("f2"));

        TextComponentUI.text = m_Label_01;
        Debug.Log("Width: " + TextComponentUI.preferredWidth + "   Height: " + TextComponentUI.preferredHeight);
    }
}

Timing on when you are executing your code might be a factor. If you could provide a simple test scene that would be most useful to figure out why you are getting those values.

I will do test tomorrow and let you know

1 Like

at 16 points
7083442--842971--upload_2021-4-27_14-2-37.png

at 24
7083442--842974--upload_2021-4-27_14-2-49.png

Using TMP Directly
at 16
7083442--842980--upload_2021-4-27_14-3-33.png

at 24
7083442--842983--upload_2021-4-27_14-4-5.png

RTL 232 then 200 then 196.13 then TMPPRo @ 196.13
7083442--842998--upload_2021-4-27_14-16-37.png
As you can see the height at 196.13 is not good since the box is higher

I will try to build you a test project

Here is a project showing using Preferred Height vs GetPreferred Value
Left is RTL TMPro and right is TMPPro

https://1drv.ms/u/s!Aulc9obmapqVwMNWyircpCHomZ3Emg?e=d3vrA8