Search Unity

TextMesh Pro (Solved) `TextMeshPro_UGUI` doesn't update on `string.Empty`

Discussion in 'UGUI & TextMesh Pro' started by Creta_Park, Oct 8, 2018.

  1. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    I wrote the code to copy the mesh data and write text animations.

    And I was working on replacing TMP with UGUI in order to convert from world space to Canvas space.

    However, there was a problem in TextMeshPro_UGUI that it worked fine in TextMeshPro.

    Setting the text property to string.Empty does not change what is displayed.

    I am wondering what the difference is, and I am looking for a way to solve this problem.

    * TextMeshPro_UGUI
    TMP UGUI stringEmpty.png

    * TextMeshPro (for World space)
    TMP not UGUI stringEmpty.png
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    How are you setting the text? Are you using the .text property or one of the SetText() variant?
     
  3. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    I tried both(text property, SetText method) style, but still not work put string.Empty
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    What version of TMP are you using?
     
  5. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    I have using 1.3.0 (Unity version is 2018.2.6f1)
    recent.png
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I'll try taking a look this afternoon and get back to you shortly thereafter.
     
  7. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
  8. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    See if the following works on your end?

    Create a TextMeshProUGUI object and attach the following script to it. Make sure you reference the text object in the public field.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4. using TMPro;
    5. public class SandBox_01 : MonoBehaviour
    6. {
    7.     public TMP_Text m_TextComponent;
    8.  
    9.     private void Awake()
    10.     {
    11.         StartCoroutine(ClearText());
    12.     }
    13.  
    14.     IEnumerator ClearText()
    15.     {
    16.         while (true)
    17.         {
    18.             m_TextComponent.text = "A line of text.";
    19.  
    20.             yield return new WaitForSeconds(1.0f);
    21.  
    22.             m_TextComponent.text = string.Empty;
    23.  
    24.             yield return new WaitForSeconds(1.0f);
    25.         }
    26.     }
    27. }
    28.  
     
  9. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76


    If TMP and TMP_UGUI is pure then fine
    (nothing access from anything, just SandBox_01 component)
    But it have occurs on VertexUpdate method, CopyVertexData method has made this problem maybe i think.

    If you sure, I can share my code privatly to you in DM (or email)
     
  10. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    I want to know what the cause is...
     
  11. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Best way for me to figure out why this might be happening in this case is to get a simple repro project / scene.
     
  12. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    I have make it simply reproduce this.

    can use
    referenceGlyphTransform
    to change the first glyph's mesh of TMP.

    And with
    doMeshChange
    to toggle true then it's update the mesh of the first character.

    and
    doClearText
    determines whether to execute the ClearText coroutine method.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using TMPro;
    4. public class SandBox_01 : MonoBehaviour
    5. {
    6.     public TMP_Text m_TextComponent;
    7.     private TMP_MeshInfo[] cachedDefaultMeshData;
    8.  
    9.     public Transform referenceGlyphTransform;
    10.  
    11.     public bool doMeshChange = false;
    12.  
    13.     public bool doClearText = true;
    14.  
    15.     private void Awake()
    16.     {
    17.         if (doClearText)
    18.             StartCoroutine(ClearText());
    19.  
    20.         TMPro_EventManager.TEXT_CHANGED_EVENT.Add((TMP) => {
    21.             if (!TMP)
    22.                 return;
    23.          
    24.             if (TMP == m_TextComponent) {
    25.                 cachedDefaultMeshData = m_TextComponent.textInfo.CopyMeshInfoVertexData();
    26.                 UpdateMesh();
    27.             }
    28.         });
    29.     }
    30.  
    31.     void Update()
    32.     {
    33.         UpdateMesh();
    34.     }
    35.  
    36.     void UpdateMesh()
    37.     {
    38.         if (!doMeshChange) return;
    39.         //only first glyph change position for test.
    40.  
    41.         var textInfo = m_TextComponent.textInfo;
    42.         var characterList = textInfo.characterInfo;
    43.  
    44.         if (characterList.Length >= 1) {
    45.             TMP_CharacterInfo charInfo = characterList[0];
    46.             if (charInfo.isVisible) {
    47.              
    48.                 int materialIndex = charInfo.materialReferenceIndex;
    49.                 int vertexIndex      = charInfo.vertexIndex;
    50.  
    51.                 int TL = vertexIndex + 1, TR = vertexIndex + 2,
    52.                     BL = vertexIndex,     BR = vertexIndex + 3;
    53.  
    54.                 Vector3[] sourceVertices  = cachedDefaultMeshData[materialIndex].vertices,
    55.                           currentVertices = textInfo.meshInfo[materialIndex].vertices;
    56.              
    57.                 Vector3 offset = (sourceVertices[TL] + sourceVertices[BR]) * 0.5f;
    58.  
    59.                 Matrix4x4 matrix = referenceGlyphTransform != null ?
    60.                                    Matrix4x4.TRS(
    61.                                        referenceGlyphTransform.localPosition,
    62.                                        referenceGlyphTransform.localRotation,
    63.                                        referenceGlyphTransform.localScale
    64.                                     ) :
    65.                                    Matrix4x4.TRS(
    66.                                        transform.localPosition,
    67.                                        transform.localRotation,
    68.                                        transform.localScale
    69.                                    );
    70.              
    71.                 currentVertices[TL] = matrix.MultiplyPoint3x4(sourceVertices[TL] - offset) + offset;
    72.                 currentVertices[TR] = matrix.MultiplyPoint3x4(sourceVertices[TR] - offset) + offset;
    73.                 currentVertices[BL] = matrix.MultiplyPoint3x4(sourceVertices[BL] - offset) + offset;
    74.                 currentVertices[BR] = matrix.MultiplyPoint3x4(sourceVertices[BR] - offset) + offset;
    75.             }
    76.  
    77.             m_TextComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Vertices);
    78.         }
    79.     }
    80.     IEnumerator ClearText()
    81.     {
    82.         while (true)
    83.         {
    84.             m_TextComponent.text = "A line of text.";
    85.             yield return new WaitForSeconds(1.0f);
    86.             m_TextComponent.text = string.Empty;
    87.             yield return new WaitForSeconds(1.0f);
    88.         }
    89.     }
    90. }
    I have attached the unity package with various experimental features to help you fix bugs.

    TMP.PNG

    Update : Changed image
     

    Attached Files:

  13. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    umm... how's it going?
     
  14. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Against a deadline for Unite in LA next week and as such haven't had time to revisit this.

    As soon as I have time, I'll take a look. Thank you again for providing the package for me to look at.
     
  15. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    Thank you for listening, I hope the bugs will be fixed as soon as possible :D
     
  16. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    Bumped this thread again, this is still not fixed on newer version of TMP...
    Tested on 2019.1.3f1 (TMP 2.0.1)

    2019.1.3f1.gif
     
  17. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I am looking through your script and there are issues.

    For instance, you can't rely on the length of the textInfo.characterInfo array is these are allocated in blocks and will almost always exceed the number of characters in the text object. You should be using textInfo.characterCount or other similar count in the textInfo class.

    I would recommend you take a look at the VertexJitter.cs script included in the TMP Examples & Extras and used by the Animating Vertex Attribute example. This will give you a good reference to learn from.

    The VertexColorCycler.cs, WarpTextExample.cs and SkewTextExample.cs scripts all make use of the UpdateVertexData() function.

    It is also important to understand that until the text object has been rendered, the textInfo class may not contain valid data. This is why in the example VertexJitter.cs script it uses ForceMeshUpdate()

    P.S. I am still looking through your example to make sure everything on the TMP side is working as expected.
     
    Last edited: May 18, 2019
  18. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    Oh, thanks to reply.
    So I discover it again, and finally understood the key to this problem.

    upload_2019-5-20_19-25-46.png

    If `TMP.characterCount` is 0, I guess TMP will only hide all created data for quick use (caching).
    If call a method for actual display, such as `TMP.UpdateGeometry`, the hidden text appears to be displayed again.

    If I understand it well, can I fix it by not calling the mesh, render, or vertex-related methods when the characterCount is zero? (Am I right?)

    The picture below is what happens when the `UpdateGeometry` method is called even though characterCount is 0. It's exactly the same thing.

    upload_2019-5-20_19-33-46.png
     
    Last edited: May 20, 2019
  19. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    Also... I wonder if TMP's behavior is different between the UGUI version and the world-space version (like the GIF above).

    Is there any reason? : P
     
  20. Creta_Park

    Creta_Park

    Joined:
    Mar 11, 2016
    Posts:
    76
    works.gif
    I putted TMP_TextInfo.characterCount equals zero then ignore update mesh logic, and now works fine.