Search Unity

TextMesh Pro Issue getting new TextMeshPro size on dynamic update

Discussion in 'Unity UI & TextMesh Pro' started by ancatut, Jan 6, 2019.

  1. ancatut

    ancatut

    Joined:
    Mar 24, 2018
    Posts:
    5
    Hi guys. I have a set of Button objects in a Canvas, each containing a TMPro UGUI element whose contents are changed during runtime. The Button objects have a ContentSizeFitter to adapt to the height of the TMPro. I need to use word wrapping because I have multiple lines of text to be shown.

    Here's my code:
    Code (CSharp):
    1.  
    2.  
    3. int i = 0;
    4.  
    5. foreach (var optionString in optionsCollection.options) {    
    6.      dialogueBubbles[i].optionButtonText.text = optionString;
    7.  
    8.      dialogueBubbles[i].optionButtonText.ForceMeshUpdate(true);
    9.      dialogueBubbles[i].optionButton.rectTransform.ForceUpdateRectTransforms();
    10.  
    11.      Debug.Log("Option Button " + i + " height: " + dialogueBubbles[i].optionButton.GetComponent<RectTransform>().rect.height);
    12.      Debug.Log("Option Button Text " + i + " height: " + " " + dialogueBubbles[i].optionButtonText.GetComponent<RectTransform>().rect.height);
    13.    
    14.      i++;
    15. }
    16.  
    The problem I'm having is that the height values printed are not updated, despite calling ForceMeshUpdate(). What I see in the console is 0 for the TMPro height and the default values set before running the game for the Buttons. Another anomaly that occurs is that the height of the text box sometimes varies by a fixed amount of padding.

    I also tried setting the size of the TMPro object manually with GetPreferredValues(text) but this only works for one line of text, as far as I can see, whereas my text needs to span across multiple lines. This is the version of the code above including GetPreferredValues():

    Code (CSharp):
    1.  
    2. int i = 0;
    3.  
    4. foreach (var optionString in optionsCollection.options) {    
    5.          
    6.      Vector2 textSize = dialogueBubbles[i].optionButtonText.GetPreferredValues(optionString);
    7.      dialogueBubbles[i].optionButtonText.text = optionString;
    8.      dialogueBubbles[i].optionButton.GetComponent<RectTransform>().sizeDelta = textSize;
    9.      dialogueBubbles[i].optionButtonText.ForceMeshUpdate(true);
    10.      dialogueBubbles[i].optionButton.rectTransform.ForceUpdateRectTransforms();
    11.  
    12.      Debug.Log("Option Button " + i + " height: " + dialogueBubbles[i].optionButton.GetComponent<RectTransform>().rect.height);
    13.      Debug.Log("Option Button Text " + i + " height: " + " " + dialogueBubbles[i].optionButtonText.GetComponent<RectTransform>().rect.height);
    14.    
    15.      i++;
    16. }
    17.  
    I'd appreciate any help.
     
    Last edited: Jan 12, 2019
  2. ancatut

    ancatut

    Joined:
    Mar 24, 2018
    Posts:
    5
    I still haven't found the reason why this might be happening, can anyone please offer any pointers?
     
  3. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    2,290
    Just wanted to let you know that I saw your initial post but simply haven't had time to look into it yet as I am focused on trying to get the next release of TMP out.

    Hopefully, you can get some assistance from another TMP user. At any rate, I'll take a look as soon as I have a chance.
     
  4. ancatut

    ancatut

    Joined:
    Mar 24, 2018
    Posts:
    5
    Much appreciated Stephan_B, I'm just a bit pressed by time to fix this issue which is why I bumped this.