Skip to content

Instantly share code, notes, and snippets.

@DarkDesire
Last active August 28, 2016 01:42
Show Gist options
  • Save DarkDesire/fac180105122348fb1ab to your computer and use it in GitHub Desktop.
Save DarkDesire/fac180105122348fb1ab to your computer and use it in GitHub Desktop.
Typying text with coroutine
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent(typeof(Text))]
public class Typer : MonoBehaviour {
public string msg = "Replace";
public float startDelay = 2f;
public float typeDelay = 0.01f;
private Text textComp;
void Start () {
StartCoroutine("TypeIn");
}
void Awake(){
textComp = GetComponent<Text>();
}
public IEnumerator TypeIn() {
yield return new WaitForSeconds(startDelay);
for (int i = 0; i < msg.Length; i++) {
textComp.text = msg.Substring(0, i+1);
yield return new WaitForSeconds(typeDelay);
}
}
public IEnumerator TypeOff() {
for (int i = msg.Length; i >= 0; i--) {
textComp.text = msg.Substring(0, i);
yield return new WaitForSeconds(typeDelay);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment