Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

TextMesh Pro Preferred width/height sometimes 0

Discussion in 'UGUI & TextMesh Pro' started by Baste, Sep 30, 2020.

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    5,416
    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:

    upload_2020-9-30_17-17-4.png

    Any idea why this would happen? I tried turning on/off "Visible Descender", but that didn't seem to change things.
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,673
    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?
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    5,416
    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:

    Code (csharp):
    1. baseSize = textPlateText.GetPreferredValues();
    Now it does this:

    Code (csharp):
    1. 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.
     

    Attached Files:

  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,673
    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.
     
    Baste likes this.
  5. RaventurnPatrick

    RaventurnPatrick

    Joined:
    Aug 9, 2011
    Posts:
    63
    Hmm I think since that change the TextMeshPro.text is changed when calling GetPreferredValues. Was this an intended change?
     
  6. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,673
    Definitely not intended.

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

    What version of Unity are you using?
     
  7. RaventurnPatrick

    RaventurnPatrick

    Joined:
    Aug 9, 2011
    Posts:
    63
    Unity 2020 LTS
     
  8. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,673
    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?
     

    Attached Files:

    Last edited: Mar 31, 2021
  9. RaventurnPatrick

    RaventurnPatrick

    Joined:
    Aug 9, 2011
    Posts:
    63
    I tried it, but unfortunately it does not fix the problem. Workaround for now is to store the original text before calling GetPreferredValues
     
  10. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,673
    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?
     
    Last edited: Apr 3, 2021
  11. Morphus74

    Morphus74

    Joined:
    Jun 12, 2018
    Posts:
    101
    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
    Code (CSharp):
    1. float currentWidth = cellSizer.rectTransform.rect.width;
    2. cellSize = cellSizer.GetPreferredValues(text, currentWidth, 0f).y;
    3. cellSizer.text = text;
    4. cellSize = cellSizer.preferredHeight;
    And the text if you are curious is dsjkfhdklsjflkdjslafjlds;ajfljdsal;kfjlkdsjaflkjdlsakfjlkdasjfl;djsalfjldkasjfl

    And the font asset https://1drv.ms/u/s!Aulc9obmapqVwL5VxA-1t2oyteYBtg?e=Dd4ZZ8
     
  12. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,673
    Let me see if I can reproduce this on my end and follow up without thereafter.
     
  13. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    5,673
    This is what I get in the Editor using the font you provided which is a height of 196.13.

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

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

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using TMPro;
    4.  
    5. public class PreferredValueCheck : MonoBehaviour
    6. {
    7.     public TextMeshProUGUI TextComponentUI;
    8.  
    9.     private string m_Label_01 = "A few lines of text for testing preferred values.";
    10.  
    11.     private void Awake()
    12.     {
    13.         Vector2 preferredValues = TextComponentUI.GetPreferredValues(m_Label_01, 155, 0);
    14.         Debug.Log(preferredValues.ToString("f2"));
    15.  
    16.         TextComponentUI.text = m_Label_01;
    17.         Debug.Log("Width: " + TextComponentUI.preferredWidth + "   Height: " + TextComponentUI.preferredHeight);
    18.     }
    19. }
    20.  
    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.
     
  14. Morphus74

    Morphus74

    Joined:
    Jun 12, 2018
    Posts:
    101
    I will do test tomorrow and let you know
     
    Stephan_B likes this.
  15. Morphus74

    Morphus74

    Joined:
    Jun 12, 2018
    Posts:
    101
    at 16 points
    upload_2021-4-27_14-2-37.png


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

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

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

    RTL 232 then 200 then 196.13 then TMPPRo @ 196.13
    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
     
  16. Morphus74

    Morphus74

    Joined:
    Jun 12, 2018
    Posts:
    101
unityunity