Search Unity

How do I get the width of text on a disabled TMP text?

Discussion in 'UGUI & TextMesh Pro' started by v-ercart, Jan 17, 2019.

  1. v-ercart

    v-ercart

    Joined:
    Apr 25, 2018
    Posts:
    8
    I want to draw a line under the width of my text, like this:
    upload_2019-1-17_9-48-45.png

    In the screenshot above, I'm using this code on my TMP object:

    lineScaler.x = GetTextWidth(Text);


    However, when the TMP is disabled (I'm hiding it because it's "scrolled" out of visibility), the GetTextWidth(Text) is always zero, even when I do Text.ForceMeshUpdate() first. So my line is way too small.

    So I tried using
    lineScaler.x = Text.preferredWidth;


    But then the line is the width of .... well, I'm not really sure what the width is. But it's something with margins that are wider than the text:
    upload_2019-1-17_9-51-42.png

    This, however, returns the same value whether the Text is enabled or disabled.

    How do I get the TextWidth like in the first picture even when the TMP object is disabled?


    I'm using Unity 2018.3 and TMP 1.3.0 from the Package Manager.