using System;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
///
/// 播放文字脚本
///
public class CharForeach : MonoBehaviour
{
public float letterPause = 1f;
private string word;
private Text text;
public delegate void CallBack();
private Action callBack;
private Action callBackFinal = null;
void Awake()
{
text = transform.Find("Text").GetComponent();
transform.Find("BGPanel").GetComponent().onClick.AddListener(OnClickBtn);
}
public void StartShowWord(string str, Action cb)
{
word = str;
text.text = "";
callBack = cb;
StartCoroutine("TypeText");
}
string[] wordArray;
int arrayIndex;
bool isStarting;
public void StartShowWord(string[] str, Action cb)
{
if (!isStarting)
{
wordArray = str;
arrayIndex = 0;
word = wordArray[arrayIndex];
text.text = "";
callBack = cb;
StartCoroutine("TypeText");
}
}
public void StartFinalShowWord(string[] str, Action cb, Action cb2)
{
if (!isStarting)
{
wordArray = str;
arrayIndex = 0;
word = wordArray[arrayIndex];
text.text = "";
callBack = cb;
callBackFinal = cb2;
StartCoroutine("TypeText");
}
}
private void OnClickBtn()
{
if (isStarting)
{
ShowWordImmediately();
}
else
{
if (wordArray != null && arrayIndex != wordArray.Length - 1)
{
arrayIndex++;
word = wordArray[arrayIndex];
text.text = "";
StartCoroutine("TypeText");
}
else
{
if (callBack != null)
{
callBack();
}
}
}
}
public void ShowWordImmediately()
{
if (isStarting)
{
StopCoroutine("TypeText");
isStarting = false;
text.text = "";
char[] charArray = word.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
SetText(charArray[i]);
}
callBackFinal?.Invoke();
}
}
private IEnumerator TypeText()
{
isStarting = true;
char[] charArray = word.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
SetText(charArray[i]);
yield return new WaitForSeconds(letterPause);
}
isStarting = false;
callBackFinal?.Invoke();
yield break;
}
private void SetText(char c)
{
if (c.Equals('$'))
{
text.text += GuideConfig.showWordsUBB[0];
}
else if (c.Equals('%'))
{
text.text += GuideConfig.showWordsUBB[1];
}
else if (c.Equals('`'))
{
text.text += GuideConfig.showWordsUBB[2];
}
else if (c.Equals('&'))
{
text.text += GuideConfig.showWordsUBB[3];
}
else if (c.Equals('*'))
{
text.text += GuideConfig.showWordsUBB[4];
}
else if (c.Equals('<'))
{
text.text += GuideConfig.showWordsUBB[5];
}
else
{
text.text += c;
}
}
}