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

Text Mesh Pro

Discussion in 'Scripting' started by Black4rrow, Feb 27, 2022.

  1. Black4rrow

    Black4rrow

    Joined:
    Nov 27, 2021
    Posts:
    6
    Hello,
    I found a script to make my Text show letter by letter, but i can't find one to make the same thing with TMP. Can anyone help me with that ?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    What class does it use to display the text right now?

    If it is the one from UnityEngine.UI then you can just change it to use the TextMeshPro class you want, either the one that uses a Canvas (UGUI) or the bare one.
     
  3. Black4rrow

    Black4rrow

    Joined:
    Nov 27, 2021
    Posts:
    6
    This is the code, I tried to change the "[RequireComponent(typeof(Text))]" to "[RequireComponent(typeof(TextMesh))]"
    but when the script run there is errors.


    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. [RequireComponent(typeof(Text))]
    6. public class LoadText : MonoBehaviour
    7. {
    8.  
    9.     private Text uiText;
    10.  
    11.     private float showSpeed = 0.05f;
    12.  
    13.     private string showText, uiTextCopy;
    14.  
    15.     private bool coroutineProtect, loadText;
    16.  
    17.  
    18.     private void Start()
    19.     {
    20.         uiText = GetComponent<Text>();
    21.  
    22.         TextInformations();
    23.     }
    24.  
    25.     private void OnEnable() { uiTextCopy = null; }
    26.  
    27.     private void Update()
    28.     {
    29.         if (loadText && !coroutineProtect)
    30.         {
    31.             StartCoroutine(LoadLetters(uiTextCopy));
    32.             coroutineProtect = true;
    33.         }
    34.  
    35.         else if (loadText && coroutineProtect) { uiText.text = showText; }
    36.  
    37.         else if (!loadText && !coroutineProtect)
    38.         {
    39.             if (uiText.text != uiTextCopy) { TextInformations(); }
    40.         }
    41.     }
    42.  
    43.     private void TextInformations()
    44.     {
    45.         uiTextCopy = uiText.text;
    46.         showText = null;
    47.         uiText.text = null;
    48.  
    49.         loadText = true;
    50.         coroutineProtect = false;
    51.     }
    52.  
    53.     private IEnumerator LoadLetters(string completeText)
    54.     {
    55.         int textSize = 0;
    56.  
    57.         while (textSize < completeText.Length)
    58.         {
    59.             showText += completeText[textSize++];
    60.             yield return new WaitForSeconds(showSpeed);
    61.         }
    62.  
    63.         coroutineProtect = false;
    64.         loadText = false;
    65.     }
    66.  
    67. }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    While I admire your adventurous spirit, you want to at least get a passing familiarity with the stuff you're trying. :)

    The
    TextMesh
    class is the ancient non-canvas way of displaying Text and has nothing to do with TextMeshPRO.

    If you want to apply what I suggest above, the class you (probably) want is
    TexMeshProUGUI
    . Don't forget to add the correct using for TMPro.
     
  5. Black4rrow

    Black4rrow

    Joined:
    Nov 27, 2021
    Posts:
    6
    Im new to Unity and C# :oops:
    Thank you for your advices. I'll try that.
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Welcome! Looking at the above code, it does not seem to properly handle rich text, which TMPro and Text supports. For instance if you embedded bold or underlined or COLORED text, it will not work properly.

    My typewriter package (attached) does handle this. If you don't need that stuff, don't bother.

    If you wanted to upgrade it to TMPro, same steps apply as the above. Full comments in the code.
     

    Attached Files:

  7. Black4rrow

    Black4rrow

    Joined:
    Nov 27, 2021
    Posts:
    6
    Wow ! Thank you so much
    I'll try later, I can't now
     
  8. Black4rrow

    Black4rrow

    Joined:
    Nov 27, 2021
    Posts:
    6
    WOW, it works !!!
    Thank you so much !!!
     
    Kurt-Dekker likes this.