Search Unity

TextMesh Pro [IN-9712] Last character of text in <mspace> closure affects UI Preferred Size

Discussion in 'UGUI & TextMesh Pro' started by Neonlyte, Jul 9, 2022.

  1. Neonlyte

    Neonlyte

    Joined:
    Oct 17, 2013
    Posts:
    516
    Bug Report ID: IN-9712

    1. What happened

    TextMeshPro has a <mspace> tag to make any non-monospace fonts characters monospace within its closure by forcing an enqual character width specified by the tag, e.g. "<mspace=0.6em>ABC</mspace>". This works well for rendered mesh, but forced width is not taken into account for the last character when calculating the Prefered Size in UGUI.

    This is most apparent for frequent changing text, like a digit counter, if the RectTransform of the text is controlled by a layout group and thus its width always set to the preferred width of TextMeshProUGUI. If the last digit/character of the string changes, the width of the entire string will change slightly and cause some visual jitter within the parent layout, which is inconsisent with the intention of monospacing that because every character's width is the same, changing any character, including the last one of a text, should not change the width of the text as long as the number of character stays the same.

    Attached is a video of the issue in action demostrated with the attached project:


    2. How can we reproduce it using the example you attached

    Using the project attached, open the SampleScene in Scenes folder and hit play. There should be two counters counting from 100000. The upper counter adds 1 per frame (i.e. changes the last digit), and the lower counter adds 10 per frame (i.e. changes the second to last digit). Notice that the width of upper counter jitters constanly while the lower counter stays stationary. You can also observe the width property of both TextMeshPro Game Objects in the Inspector and see that the width of the upper counter text constantly changing.