Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Issue about modify meshInfo in the same frame in which TMP_Text is created

Discussion in 'UGUI & TextMesh Pro' started by giresharu, Aug 21, 2023.

  1. giresharu

    giresharu

    Joined:
    Oct 11, 2015
    Posts:
    25
    As it is well known, we can modify the meshInfo of the textInfo property of TextMeshPro’s TMP_Text to change properties like vertex colors of existing meshes. For example, the following code snippet:
    Code (CSharp):
    1.     Button button;
    2.     TMP_Text tmp;
    3.    
    4.     void Awake() {
    5.         button = GetComponent<Button>();
    6.         button.onClick.AddListener(CreateText);
    7.         tmp = new GameObject("TMP_Text").AddComponent<TextMeshProUGUI>();
    8.         tmp.transform.SetParent(FindObjectOfType<Canvas>().transform);
    9.     }
    10.  
    11.     void CreateText() {
    12.         tmp.SetText("This is a test sentence");
    13.  
    14.         tmp.ForceMeshUpdate();
    15.         TMP_TextInfo textInfo = tmp.textInfo;
    16.         TMP_MeshInfo[] meshInfos = tmp.textInfo.meshInfo;
    17.         foreach (var info in meshInfos) {
    18.             for (int index = 0; index < info.colors32.Length; index++) {
    19.                 info.colors32[index] = new Color32(255, 0, 0, 255);
    20.             }
    21.         }
    22.        
    23.         tmp.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
    24.     }
    This allows us to set the text to be displayed and modify the text to appear in red color after clicking a button, and then refresh it on the canvas.

    However, I’m currently facing an issue where if I place the creation of the TMP_Text and the code to modify the mesh within the same frame, the changes to the mesh get reverted. For instance:
    Code (CSharp):
    1. Button button;
    2.  
    3.     void Awake() {
    4.         button = GetComponent<Button>();
    5.         button.onClick.AddListener(CreateText);
    6.     }
    7.  
    8.     void CreateText() {
    9.         TMP_Text tmp = new GameObject("TMP_Text").AddComponent<TextMeshProUGUI>();
    10.         tmp.transform.SetParent(FindObjectOfType<Canvas>().transform);
    11.         tmp.SetText("This is a test sentence");
    12.  
    13.         tmp.ForceMeshUpdate();
    14.         TMP_TextInfo textInfo = tmp.textInfo;
    15.         TMP_MeshInfo[] meshInfos = tmp.textInfo.meshInfo;
    16.         foreach (var info in meshInfos) {
    17.             for (int index = 0; index < info.colors32.Length; index++) {
    18.                 info.colors32[index] = new Color32(255, 0, 0, 255);
    19.             }
    20.         }
    21.        
    22.         tmp.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
    23.     }
    While debugging with breakpoints, I’ve confirmed that meshInfo.colors32 is indeed being modified. But as the execution continues, at some point, meshInfo.colors32 seems to revert back to its original state (the TextMeshPro codebase is quite extensive, and I don’t have the capacity to identify the exact issue). My only assumption is that there might be a refresh operation on meshInfo at the end of the frame after the creation of TMP_Text, causing the reset.

    Is there any way I can prevent this reset from happening or, in other words, what approach should I take to immediately modify meshInfo in the same frame in which TMP_Text is created?