Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Different text mesh pro sizings when rednering the same text, on a dynamically sized window

Discussion in 'UGUI & TextMesh Pro' started by SassyPantsy, Nov 3, 2023.

  1. SassyPantsy

    SassyPantsy

    Joined:
    May 17, 2020
    Posts:
    141
    Hi all.
    I've been following code monkey's tutorial for creating a canvas space tooltip, as here -


    It works flawlessly, but I wanted to add the ability of setting a maximum text box width.
    This is my current code -

    Code (CSharp):
    1. private void SetText(string input)
    2. {
    3.    text.text = input;
    4.    text.ForceMeshUpdate();
    5.    Vector2 textSize = text.GetRenderedValues(false);
    6.    text.ForceMeshUpdate();
    7.    backgroundRectTransform.sizeDelta = textSize + paddingSize;
    8.    text.rectTransform.sizeDelta = backgroundRectTransform.sizeDelta;
    9.    if (text.rectTransform.sizeDelta.x >= maxWidth)
    10.    {
    11.        text.enableWordWrapping = true;
    12.    }
    13.    else
    14.    {
    15.        text.enableWordWrapping = false;
    16.    }
    17. }
    Just for a quick overview of the entire tooltip system -
    when the cursor hovers above a ui element, a tooltip appears. The tooltip resizes based on the input text. OnMouseExit, the tooltip disables:

    Code (CSharp):
    1.  
    2. public static void ShowTooltip(string tooltipText)
    3. {
    4.    instance.transform.SetAsLastSibling();
    5.    instance.ToggleTooltip(true);
    6.    instance.SetText(tooltipText);
    7. }
    8. public static void HideTooltip()
    9. {
    10.    instance.ToggleTooltip(false);
    11. }
    12.  
    It works alright, with the only caveat being, that whenever I hover over the same ui element (that has the same text input), each time show tooltip is called, the tooltip has a different size.
    The first time, it has no padding at all. The second time, it has correct width padding, but incorrect (too much) height padding. From the third time onward, it has correct sizing and padding.
    If it hover over an element with different text, and return to the original one, the above happens the same.

    I understand it has something to do with the tmp size properties before and after setting the word wrapping, but I've played around with recalculating textSize and ForceMeshUpdate-ing again, and nothing seem to work.

    Can anybody help me with this?
     
    Last edited: Nov 3, 2023