欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UGUI打字机效果

程序员文章站 2022-06-22 20:01:43
...
using UnityEngine;
using UnityEngine.UI;
public class TypeEffect : MonoBehaviour {
    float letterpause = 0.2f;
    AudioClip clip;
    private string word;
    private string text = "sdfdsgfghrturjhgfbsgdasf";
    public Text showtext;
    // Use this for initialization
    IEnumerator Start () {
        word = text;
        text = null;
        yield return new WaitForSeconds(2f);
      StartCoroutine (TypeWord());
    }
    IEnumerator  TypeWord()
    {
        foreach (var item in word.ToCharArray())
        {
            showtext.text += item;
            this.GetComponent<AudioSource>().PlayOneShot(clip, 1f);
            yield return new WaitForSeconds(letterpause);
        }
    }
}