UGUI打字机效果文本组件

2023-07-29,,

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);
}
}
}

  

UGUI打字机效果文本组件的相关教程结束。

《UGUI打字机效果文本组件.doc》

下载本文的Word格式文档,以方便收藏与打印。