wangguan
2020-10-23 f08d4cf99c556eee7ba06f99015bf87a601dad50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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;
        }
    }
}