using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class AutoText : MonoBehaviour
{
string str = ""; //要显示的所有字符串
string s = ""; //每次显示的字符串
float time = 0.05f; //每个文字显示时间间隔
Text text;
Coroutine cor;
void Update()
{
//开启播放后,点击鼠标左键或任意按键则显示全部文本
if (cor != null && (Input.GetMouseButtonDown(0) || Input.anyKeyDown))
{
Finish();
}
}
/// <summary>
/// 调用协成实现一个字一个字冒出的效果
/// </summary>
public void PlayText(string content = null, float time = 0.05f)
{
text = GetComponent<Text>();
str = text.text;
//重置
if (!string.IsNullOrEmpty(content))
{
str = content;
}
this.time = time;
text.text = "";
s = "";
//当前有协成,则先关闭
if (cor != null)
{
StopCoroutine(cor);
}
//开启协成
cor = StartCoroutine(ShowText(str.Length));
}
IEnumerator ShowText(int strLength)
{
int i = 0;
while (i < strLength)
{
yield return new WaitForSeconds(time);
s += str[i].ToString();
text.text = s;
i += 1;
}
//显示完成,停止协成
Finish();
}
//立刻显示所有文字
public void Finish()
{
s = str;
text.text = s;
if (cor != null)
{
StopCoroutine(cor);
cor = null;
}
}
}
调用
/// <summary>
/// 逐字播放
/// </summary>
public static void PlayAutoText(GameObject go, string content = null, float time = 0.05f)
{
if (go != null)
{
Text text = go.GetComponent<Text>();
if (text != null)
{
AutoText autoText = go.GetComponent<AutoText>();
if (autoText == null)
{
autoText = go.AddComponent<AutoText>();
}
autoText.PlayText(content, time);
return;
}
}
}
|