Search Unity

TextMesh Pro GetPreferredValues Optimization?

Discussion in 'UGUI & TextMesh Pro' started by mitaywalle, Oct 29, 2020.

  1. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    247
    Hello! Thank you, TMPro is great complex system

    - Unity 2019.2.2f1
    - TMPro 2.0.1

    When I need to resize background to fit it to text, I use TMP_Text.GetPreferredValues(), and What is strange for me - that if DeepProfile doesn't lie, it make doubled work. CalculatePreferredValues() execute in two paths.

    My questions are:
    - can I somehow optimize this process?
    - maybe there's another function in TMPro, that calculate sizes and get it to me?

    Screenshot_6.png
     
    Last edited: Oct 29, 2020
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    In order to calculate the Preferred Values, an horizontal layout pass first occurs to determine the preferred width and then using the results of this horizontal pass, a vertical layout pass is needed to determine the preferred height. See the following and section related to Layout Calculations.

    The preferred height cannot be computed without a horizontal pass first.

    I did make some minor tweaks to this in the latest release which is version 2.1.3.

    Accessing the text components, TMP_Text.preferredWidth and TMP_Text.preferredHeight should avoid doing layout passes provided the text properties have not changed. Ie. it would return the cached values.
     
    mitaywalle likes this.
  3. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    247
    Thank you for fast answer. Where possible, I should use preferredHeight / Width, got it.