/*--------------------------------------------------------------------
- Author Name: DXL
- Creation Time: 2019.1.2
- File Describe: UGUI的text文字逐渐显示
- ------------------------------------------------------------------*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using LogPrint;
public class TextDisplay : MonoBehaviour
{
#region 控件内部逻辑
public float displayTime; //文字全部显示所需的时间
public bool loop = false; //是否循环播放,注意,在这行前就开始设定,中途只能关,不能开启
string textStr = ""; //保存原来的文字信息
Text strText; //text的控件
float showSpeed = 0.15f; //每个字的显示时间
char[] strChars; //保存文字的数组
void Start()
{
strText = GetComponent<Text>();
if (strText == null)
{
LogSystem.LogError("Text component not find!");
return;
}
textStr = strText.text;
if (string.IsNullOrEmpty(textStr))
{
textStr = "TMD,Text str is empty!";
}
strChars = textStr.ToCharArray();
strText.text = "";
//根据字符计算速度,不填的话,就是0.15秒显示一个字
showSpeed = displayTime / strChars.Length <= 0 ? showSpeed : displayTime / strChars.Length;
StartCoroutine(ShowStr());
}
IEnumerator ShowStr()
{
for (int i = 0; i < strChars.Length; ++i)
{
yield return new WaitForSeconds(showSpeed);
strText.text += strChars[i];
}
if(loop)
{
strText.text = "";
yield return StartCoroutine(ShowStr());
}
}
#endregion
#region 对外接口
void AddDisplay()
{
}
#endregion
}