using System;
|
using UnityEngine;
|
using UnityEngine.UI;
|
using System.Collections;
|
/// <summary>
|
/// 播放文字脚本
|
/// </summary>
|
public class CharForeach : MonoBehaviour
|
{
|
public float letterPause = 1f;
|
private string word;
|
private Text text;
|
public delegate void CallBack();
|
private Action callBack;
|
void Awake()
|
{
|
text = transform.Find("Scroll View/Viewport/Content/Text").GetComponent<Text>();
|
}
|
|
public void StartShowWord(string str, Action cb)
|
{
|
Debug.Log("StartShowWord"+str);
|
|
word = str;
|
text.text = "";
|
callBack = cb;
|
StartCoroutine("TypeText");
|
}
|
|
public void ShowWordImmediately()
|
{
|
Debug.Log("ShowWordImmediately");
|
StopCoroutine("TypeText");
|
text.text = word;
|
if (callBack != null)
|
{
|
callBack();
|
callBack = null;
|
}
|
|
}
|
|
private IEnumerator TypeText()
|
{
|
foreach (char letter in word.ToCharArray())
|
{
|
text.text += letter;
|
yield return new WaitForSeconds(letterPause);
|
}
|
|
if (callBack != null)
|
{
|
callBack();
|
callBack = null;
|
}
|
}
|
}
|