Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Unity UI toolkit With TextMeshPro

Discussion in 'UI Toolkit' started by RaulPluto, Jun 14, 2023.

  1. RaulPluto

    RaulPluto

    Joined:
    Jan 21, 2023
    Posts:
    11
  2. RaulPluto

    RaulPluto

    Joined:
    Jan 21, 2023
    Posts:
    11
    Now I have this on canvas, ¿how could I do it with toolkit?:
    upload_2023-6-14_21-28-20.png
    upload_2023-6-14_21-28-36.png
     
  3. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    91
    There is a Label component in UI Toolkit which works with text.
    You can apply Text style to the Label to change appearance. It's located in the middle of Inlined Styles foldout.
    Font assets are basically the same, but slightly different.
     
  4. RaulPluto

    RaulPluto

    Joined:
    Jan 21, 2023
    Posts:
    11
    Okay I have another question.
    I want to access the vertices like in TextMeshPro ¿how can I do it?
    I have something like this
    Code (CSharp):
    1.  
    2. public IEnumerator TypeText(MonoBehaviour box, SentenceSo sentenceSo, TMP_Text textMesh)
    3.         {
    4.             SubscribeToInputSystem();
    5.  
    6.             // Stores sentence as a private attribute
    7.             _sentenceSo = sentenceSo;
    8.  
    9.             // Apply text effects in a loop
    10.             box.StartCoroutine(sentenceSo.ContinueApplyingEffects(textMesh));
    11.  
    12.             foreach (var character in _sentenceSo.Text)
    13.             {
    14.                 // Writes new character
    15.                 textMesh.text += character;
    16.  
    17.                 // Applies effects just after adding a character
    18.                 yield return sentenceSo.ApplyTextEffects(textMesh);
    19.  
    20.                 // Waits for the next character to be written depending on the script
    21.                 yield return new WaitForSeconds(1/GetSpeed(character));
    22.             }
    23.  
    24.             UnsubscribeToInputSystem();
    25.         }
    26.  
    That calls something like that:
    Code (CSharp):
    1. public IEnumerator ApplyTextEffects(TMP_Text textMesh)
    2.         {
    3.             textMesh.ForceMeshUpdate();
    4.  
    5.             // Does not apply text effects if they are not defined
    6.             if (textEffects.Length == 0) yield break;
    7.  
    8.             // Gets text effects if necessary
    9.             if (_effectsByIndex == null) GetTextEffects();
    10.  
    11.             // Gets current mesh and mesh data
    12.             Mesh mesh = textMesh.mesh;
    13.             var vertices = mesh.vertices;
    14.             var colours = mesh.colors;
    15.  
    16.             // Loops over each character currently on the text mesh
    17.             for (var i = 0; i < textMesh.textInfo.characterCount; i++)
    18.             {
    19.                 // Checks if an effect has to be applied
    20.                 if (' ' == Text[i]) continue;
    21.                 if (!HasToApplyTextEffect(i, out AEffectCoverage coverage)) continue;
    22.  
    23.                 // Gets current offsets
    24.                 var currentOffsets = coverage.ApplyEffect(i);
    25.  
    26.                 // Gets character initial vertex index
    27.                 var index = textMesh.textInfo.characterInfo[i].vertexIndex;
    28.  
    29.                 // Updates vertices
    30.                 vertices[index]     += currentOffsets[0];
    31.                 vertices[index + 1] += currentOffsets[1];
    32.                 vertices[index + 2] += currentOffsets[2];
    33.                 vertices[index + 3] += currentOffsets[3];
    34.  
    35.                 // Updates colours
    36.                 Color textColour = coverage.TextEffect.TextColour;
    37.                 colours[index]     = textColour;
    38.                 colours[index + 1] = textColour;
    39.                 colours[index + 2] = textColour;
    40.                 colours[index + 3] = textColour;
    41.             }
    42.  
    43.             // Resets effects
    44.             foreach (AEffectCoverage c in _effectsByIndex.Values) c.ResetEffect();
    45.  
    46.             // Overrides vertices, colours and mesh
    47.             mesh.vertices = vertices;
    48.             mesh.colors = colours;
    49.             textMesh.canvasRenderer.SetMesh(mesh);
    50.  
    51.             // Waits one frame
    52.             yield return null;
    53.         }
    ¿How can I do it?

    Thanks.
     
  5. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    91
    I believe you can't animate text vertices yet in UI Toolkit