Search Unity

  1. Full schedule for #UniteBerlin is now available! Featuring talks on our roadmap, hands-on labs and much more! Check it out!
    Dismiss Notice
  2. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  3. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

TextMesh Pro TextMesh Pro: Text shall disappear after x seconds it has appeared

Discussion in 'Unity UI & TextMesh Pro' started by florianfeiler2, May 16, 2018.

  1. florianfeiler2

    florianfeiler2

    Joined:
    May 7, 2018
    Posts:
    7
    Hey folks, I'm using the TextMesh Pro Plugin and I want a that the text disappears after x seconds it completly appeared. I tried the following code, but it immediatley delets the letters.
    Code (CSharp):
    1. public class Dialog : MonoBehaviour
    2. {
    3.     public TextMeshProUGUI textDisplay;
    4.     public string[] sentences;
    5.     private int index;
    6.     public float typingSpeed;
    7.     public GameObject continueButton;
    8.     public Animator textDisplayAnim;
    9.  
    10.     void Start()
    11.     {
    12.         StartCoroutine(Type());
    13.     }
    14.     void Update()
    15.     {
    16.        
    17.         if(textDisplay.text == sentences[index])
    18.         {
    19.             continueButton.SetActive(true);
    20.         }
    21. //Here I tried to let the text go away, but it immediatley deletes the first letter which was written
    22.         if(sentences.Length - 1 == index)
    23.         {
    24.  
    25.             textDisplay.text = "";
    26.        StartCoroutine(Type());
    27. }
    28.  
    29.     }
    30.     IEnumerator Type()
    31.     {
    32.         foreach(char letter in sentences[index].ToCharArray())
    33.         {
    34.             textDisplay.text += letter;
    35.             yield return new WaitForSeconds(typingSpeed);
    36.         }
    37.     }
    38.    
    39.     public void NextSentence()
    40.     {
    41.         textDisplayAnim.SetTrigger("change");
    42.         continueButton.SetActive(false);
    43.         if(index < sentences.Length - 1)
    44.         {
    45.             index++;
    46.             textDisplay.text = "";
    47.             StartCoroutine(Type());
    48.         }
    49.         else
    50.         {
    51.             textDisplay.text = "";
    52.             continueButton.SetActive(false);
    53.         }
    54.     }
    55. }
    56.  
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    1,213
    You want to avoid doing any string concatenation as this generates Garbage Collection. See example 17 Old Computer Terminal included with TextMesh Pro. This uses the maxVisibleCharacter property.

    Also take a look at this thread which also includes a link to a post that contains a script to control fading of characters over time.