using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Threading.Tasks;
public class TypewriterEffect : MonoBehaviour
{
public float charsPerSecond = 0.2f;
public float sectionPauseTime = 0.2f;
public float symbolPauseTime = 0.5f;
public string[] allShowText;
private void Start()
{
TypeWritterAsync();
}
public async void TypeWritterAsync()
{
var myText = GetComponent<Text>();
for (int i = 0; i < allShowText.Length; i++)
{
myText.text = "";
var currentShow = "\u3000\u3000" + allShowText[i];
for (int j = 0; j < currentShow.Length; j++)
{
await Task.Delay((int)(charsPerSecond * 1000));
myText.text += currentShow[j];
if (",.,。".IndexOf(currentShow[j]) != -1
&& j != currentShow.Length - 1)
{
await Task.Delay((int)(symbolPauseTime * 1000));
}
}
await Task.Delay((int)(sectionPauseTime * 1000));
}
}
}
在这里插入代码片
|