Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

TextMesh Pro Perspective effect breaking auto-size font

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

  1. tnordstrom


    Jun 19, 2019
    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.