Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMesh Pro Perspective effect breaking auto-size font

Discussion in 'UGUI & TextMesh Pro' started by tnordstrom, Aug 20, 2019.

  1. tnordstrom

    tnordstrom

    Joined:
    Jun 19, 2019
    Posts:
    2
    I am trying to make text that is perspective to make it look a little 3D. I am using `TextMeshProUGUI`. The text is on a button that changes text based on state. The code I have written works great in every state except the first one where it breaks something with the dynamic font size. This code is run from the `Start` function of the game object and also when the button pops back up:
    Code (CSharp):
    1. buttonText.text = "SPIN";
    2. buttonText.outlineWidth = 0.0f;
    3. buttonText.ForceMeshUpdate();
    4. TMP_MeshInfo[] materials = buttonText.textInfo.meshInfo;
    5. for (int i = 0; i < materials.Length; ++i)
    6. {
    7.     for (int j = 0; j < materials[i].vertexCount; ++j)
    8.     {
    9.         if (j % 4 == 1 || j % 4 == 2)
    10.         {
    11.             materials[i].vertices[j].x *= 0.9f;
    12.         }
    13.     }
    14. }
    15. buttonText.UpdateVertexData();
    This code runs when you hold down the button:
    Code (CSharp):
    1. buttonText.text = "STOP";
    2. buttonText.outlineWidth = 0.2f;
    3. // same vertex changing code here
    Here are pictures to show the issue. Please forgive my placeholder art.
    Screen Shot 2019-08-20 at 10.56.47 AM.png
    Screen Shot 2019-08-20 at 10.56.55 AM.png
    Screen Shot 2019-08-20 at 10.57.04 AM.png
    As you can see the first time text is rendered the font size is too small, but all subsequent times the dynamic font size works fine. From a little poking around it seems like the `ForceMeshUpdate` call is the one that breaks the font size, but it is necessary for the vertex changes to work.
    I hoping that somebody might have insight on a way to get the text to adjust the font size like normal on the first call to my vertex-adjusting code.