Last active
August 28, 2016 01:42
-
-
Save DarkDesire/fac180105122348fb1ab to your computer and use it in GitHub Desktop.
Typying text with coroutine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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