Search Unity

Question Rendered size of TMP_Text is 2x too large?

Discussion in 'UGUI & TextMesh Pro' started by ChickensAreDinosaurs, Mar 9, 2023.

  1. ChickensAreDinosaurs

    ChickensAreDinosaurs

    Joined:
    May 11, 2021
    Posts:
    17
    Hello all,

    I am working on in-game tooling to highlight instances where text overflows its container. I am trying to get the actual rendered size of text using TextMesh Pro and I am finding that all of the approaches I try are incorrect. Using TMP_Text.renderedHeight, textBounds, bounds, GetRenderedValues, all seem to produce bounds that are inflated x2(?) from the actual rendered text, so almost every instance of text is flagged as a false positive.

    Why are all of these returning too-large values? I'm reluctant to simply divide these numbers by 2 because that seems incorrect. In testing it's much better, but still not 100% accurate.

    I'm suspecting I can calculate the bounds myself by going char by char, but that also seems like something I shouldn't have to do.

    For further context, I'm detecting overflow like this:
    Code (CSharp):
    1.  
    2. var rectTransform = tmpText.rectTransform;
    3. return tmpText.renderedWidth >= rectTransform.rect.width || tmpText.renderedHeight >= rectTransform.rect.height;
    4.  
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Those values should be correct. You can add the TMP_TextInfoDebugTool.cs script to any text object to view the mesh and text bounds of the text to verify. This script is contained in the TMP Examples & Extras.

    As per the doc, extents are always half the size of the bounds. Next, be mindful that the position of the transform affects the bounds. For instance, if you were to move the position of the transform / pivot outside of the region of text, the bounds would grow to always encompass the transform position.
     
    ChickensAreDinosaurs likes this.