Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

TextMesh Pro New versions of TextMeshPro ParseText on every layout recalculation

Discussion in 'UGUI & TextMesh Pro' started by Rafael_CS, Apr 17, 2021.

  1. Rafael_CS


    Sep 16, 2013
    Hello @Stephan_B i found a possible bug in TMP_Text logic since version 2.1.3

    the method GetPreferredWidth() and GetPreferredHeight() is calling ParseInput() every time so every layout recalc will call it 2 times

    In version 2.1.1 we handled it with correct logic checking if (m_isInputParsingRequired || m_isTextTruncated)

    Code (CSharp):
    3.         protected float GetPreferredWidth()
    4.         {
    5.            ....
    6.             if (m_isInputParsingRequired || m_isTextTruncated)
    7.             {
    8.                 m_isCalculatingPreferredValues = true;
    9.                 ParseInputText();
    10.             }
    11.             ...
    12.             return preferredWidth;
    13.         }
    16.         protected float GetPreferredHeight()
    17.         {
    18.             ...
    19.             if (m_isInputParsingRequired || m_isTextTruncated)
    20.             {
    21.                 m_isCalculatingPreferredValues = true;
    22.                 ParseInputText();
    23.             }
    24.             ...
    25.             return preferredHeight;
    26.         }
    In version 2.1.3 or greater we have an incorrect behaviour that we always call

    m_isCalculatingPreferredValues = true;

    with this behaviour text will be parse two times on every layout recalc (For CalculateLayoutHorizontal and CalculateLayoutVertical), and will be reparsed even when we try to check the current preferred value of text component (inputText.preferredHeight for example)

    Best Regards
  2. Stephan_B


    Unity Technologies

    Feb 26, 2017
    The reason it is re-parsed is because some of the internal data structures are now shared between all text objects. As such, there is no guarantee this internal data is relevant to the current text object. This change was made to reduce to memory overhead of text objects.

    I will take a look to see if in some cases, I can skip the re-parsing like in the case of GetPreferredValues() as the underlaying PreferredWidth and PreferredHeight are called back to back where the internal data structures should be unchanged.

    P.S. I will be making further improvements to the parsing and more specifically SetArraySizes in the coming preview releases which will make those more efficient. The longer term plan is also to have the text parsing and layout become a service which will further reduce memory overhead along with performance improvements as well.
    Rafael_CS likes this.