Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMesh Pro GetPreferredValues() method seems to not take font into account.

Discussion in 'Assets and Asset Store' started by phearbot, Oct 2, 2017.

  1. phearbot

    phearbot

    Joined:
    Jul 19, 2017
    Posts:
    5
    I'm using TextMesh Pro to handle messages in a chat. The problem I'm running into is I'll run the GetPreferredValues() method and the y value of the vector being returned is right the vast majority of the time, but occasionally it returns a value too low, by one line worth of height, causing the message to overflow or truncate depending on how I have it configured.

    Changing fonts changes where the issue occurs. For example, if I have a sampling of 20 messages of various lengths the issue will occur on messages 2 and 7 of one font but on messages 6 and 13 on another. If I tweak the message lengths by a few characters they will be handled correctly.

    My best guess as to what is happening is the font being used isn't being taken into account or is possibly being taken into account incorrectly. I try to peek the definition of the GetPreferredValues method, but it looks like I can't actually see the code, which from Googling I think is because it's in a .dll.

    My actual call is:
    return msg.GetPreferredValues(text, game.ChatWidth, 0).y;​

    The text string is the content that is varying in length here, the game.ChatWidth is the width of the container these fit in to. I'm trying to get the height so I'm providing a 0 value as recommended here.

    Note: I also thought it could be related to some color tags I had in there, but ruled this out by adding a bunch of tags in a separate test string and comparing the outputs. No amount of tags change the value, which seems to be correct behavior.

    Has anyone else run into this and potentially have a workaround? Is my assumption on fonts way off? Happy to provide more context if it will help!
     
  2. phearbot

    phearbot

    Joined:
    Jul 19, 2017
    Posts:
    5
    Testing some more I'm thinking fonts are taken into account, so I'm not sure why I'm seeing what I'm seeing:

    Testing Code:
    Code (CSharp):
    1. string lll = "llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll";
    2. string MMM = "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM";
    3. if (msg.GetPreferredValues(lll, 936, 0).y != msg.GetPreferredValues(MMM, 936, 0).y)
    4. {
    5.     Debug.Log("GetPreferred lll: " + msg.GetPreferredValues(lll, 936, 0).y);
    6.     Debug.Log("GetPreferred MMM: " + msg.GetPreferredValues(MMM, 936, 0).y);
    7. }
    Using a custom font imported into TMP::
    GetPreferred lll: 40.04
    GetPreferred MMM: 70.06

    Using Anton SDF (provided TMP font):
    GetPreferred lll: 54.03
    GetPreferred MMM: 142.08

    Using Bangers SDF (provided TMP font):
    GetPreferred lll: 41.91
    GetPreferred MMM: 73.81

    Seems like it's probably user error somewhere, but I'm fairly perplexed.
     
  3. phearbot

    phearbot

    Joined:
    Jul 19, 2017
    Posts:
    5
    Well, if anyone finds this, the answer is I'm an idiot. I had 5px margins that were not being taken into account when providing the width to the method. Taking them into account resolves this issue.
     
    unity_awUpacKcgHt1UA and namesdu like this.
  4. su9257

    su9257

    Joined:
    Jun 13, 2017
    Posts:
    27
    I also encountered the same problem. Thanks to your answer. I hope the API comments can be more perfect.
     
    unity_awUpacKcgHt1UA likes this.
  5. gnovos

    gnovos

    Joined:
    Apr 29, 2020
    Posts:
    30
    I, too, am an idiot. Thank you for your last comment phearbot, that saved the day!
     
    unity_awUpacKcgHt1UA likes this.