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 Coroutine not working after disabling / re-enabling gameobject [2017.3.0f3]

Discussion in 'UGUI & TextMesh Pro' started by LandHT, Dec 30, 2017.

  1. LandHT

    LandHT

    Joined:
    Feb 3, 2017
    Posts:
    84
    Hi Unity,

    I'm trying to extend the warp example of Text Mesh Pro.

    I can't make it work after disabling / enabling the gameobject, right now it only works the first time:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using TMPro;
    4.  
    5. public class TMProWarpped : MonoBehaviour {
    6.  
    7.     private TMP_Text m_TextComponent;
    8.  
    9.     public AnimationCurve VertexCurve = new AnimationCurve (
    10.         new Keyframe (0, 0),
    11.         new Keyframe (0.25f, 2.0f),
    12.         new Keyframe (0.5f, 0),
    13.         new Keyframe (0.75f, 2.0f),
    14.         new Keyframe (1, 0f)
    15.     );
    16.     public float AngleMultiplier = 1f;
    17.     public float SpeedMultiplier = 1f;
    18.     public float CurveScale = 1f;
    19.  
    20.     private Coroutine coroutine;
    21.  
    22.     private void OnEnable () {
    23.         coroutine = null;
    24.         m_TextComponent = gameObject.GetComponent<TMP_Text> ();
    25.         coroutine = StartCoroutine (WarpText ());
    26.     }
    27.        
    28.     IEnumerator WarpText () {
    29.         VertexCurve.preWrapMode = WrapMode.Clamp;
    30.         VertexCurve.postWrapMode = WrapMode.Clamp;
    31.  
    32.         Vector3[] vertices;
    33.         Matrix4x4 matrix;
    34.  
    35.         m_TextComponent.havePropertiesChanged = true;
    36.  
    37.         float old_CurveScale = CurveScale;
    38.         AnimationCurve old_curve = CopyAnimationCurve (VertexCurve);
    39.  
    40.         while (true) {
    41.             if (!m_TextComponent.havePropertiesChanged && old_CurveScale == CurveScale && old_curve.keys[1].value == VertexCurve.keys[1].value) {
    42.                 yield return null;
    43.                 continue;
    44.             }
    45.  
    46.             old_CurveScale = CurveScale;
    47.             old_curve = CopyAnimationCurve (VertexCurve);
    48.  
    49.             m_TextComponent.ForceMeshUpdate ();
    50.  
    51.             TMP_TextInfo textInfo = m_TextComponent.textInfo;
    52.             int characterCount = textInfo.characterCount;
    53.  
    54.             if (characterCount == 0) continue;
    55.  
    56.             float boundsMinX = m_TextComponent.bounds.min.x;
    57.             float boundsMaxX = m_TextComponent.bounds.max.x;
    58.  
    59.             for (int i = 0; i < characterCount; i++) {
    60.                 if (!textInfo.characterInfo [i].isVisible) {
    61.                     continue;
    62.                 }
    63.  
    64.                 int vertexIndex = textInfo.characterInfo [i].vertexIndex;
    65.  
    66.                 int materialIndex = textInfo.characterInfo [i].materialReferenceIndex;
    67.  
    68.                 vertices = textInfo.meshInfo [materialIndex].vertices;
    69.  
    70.                 Vector3 offsetToMidBaseline = new Vector2 ((vertices [vertexIndex + 0].x + vertices [vertexIndex + 2].x) / 2, textInfo.characterInfo [i].baseLine);
    71.  
    72.                 vertices[vertexIndex + 0] += -offsetToMidBaseline;
    73.                 vertices[vertexIndex + 1] += -offsetToMidBaseline;
    74.                 vertices[vertexIndex + 2] += -offsetToMidBaseline;
    75.                 vertices[vertexIndex + 3] += -offsetToMidBaseline;
    76.  
    77.                 float x0 = (offsetToMidBaseline.x - boundsMinX) / (boundsMaxX - boundsMinX);
    78.                 float x1 = x0 + 0.0001f;
    79.                 float y0 = VertexCurve.Evaluate (x0) * CurveScale;
    80.                 float y1 = VertexCurve.Evaluate (x1) * CurveScale;
    81.  
    82.                 Vector3 horizontal = new Vector3 (1, 0, 0);
    83.                 Vector3 tangent = new Vector3 (x1 * (boundsMaxX - boundsMinX) + boundsMinX, y1) - new Vector3 (offsetToMidBaseline.x, y0);
    84.  
    85.                 float dot = Mathf.Acos (Vector3.Dot (horizontal, tangent.normalized)) * 57.2957795f;
    86.                 Vector3 cross = Vector3.Cross (horizontal, tangent);
    87.                 float angle = cross.z > 0 ? dot : 360 - dot;
    88.  
    89.                 matrix = Matrix4x4.TRS(new Vector3(0, y0, 0), Quaternion.Euler(0, 0, angle), Vector3.one);
    90.  
    91.                 vertices[vertexIndex + 0] = matrix.MultiplyPoint3x4 (vertices[vertexIndex + 0]);
    92.                 vertices[vertexIndex + 1] = matrix.MultiplyPoint3x4 (vertices[vertexIndex + 1]);
    93.                 vertices[vertexIndex + 2] = matrix.MultiplyPoint3x4 (vertices[vertexIndex + 2]);
    94.                 vertices[vertexIndex + 3] = matrix.MultiplyPoint3x4 (vertices[vertexIndex + 3]);
    95.  
    96.                 vertices[vertexIndex + 0] += offsetToMidBaseline;
    97.                 vertices[vertexIndex + 1] += offsetToMidBaseline;
    98.                 vertices[vertexIndex + 2] += offsetToMidBaseline;
    99.                 vertices[vertexIndex + 3] += offsetToMidBaseline;
    100.             }
    101.             m_TextComponent.UpdateVertexData ();
    102.  
    103.             yield return new WaitForSeconds (0.025f);
    104.         }
    105.     }
    106.  
    107.     private AnimationCurve CopyAnimationCurve (AnimationCurve curve) {
    108.         AnimationCurve newCurve = new AnimationCurve ();
    109.  
    110.         newCurve.keys = curve.keys;
    111.  
    112.         return newCurve;
    113.     }
    114. }
    Any ideas?