using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; public class TypewriterText : MonoBehaviour {
private Text text;
private string content;
private float delay;
// Use this for initialization
void Start () {
text = gameObject.GetComponent<Text>();
if(text == null)
{
Debug.LogError("没添加Text脚本");
}
} public void TypeShow(string txt, float _delay=0.5f)
{
content = txt;
delay = _delay;
StartCoroutine(AppearText());
}
public void AllShow(string txt)
{
StopAllCoroutines();
text.text = txt;
}
private IEnumerator AppearText()
{
char[] arr = content.ToCharArray();
for(int i = 0; i<arr.Length; i++)
{
text.text += arr[i];
yield return new WaitForSeconds(delay);
}
}
}