The Text Typewriter Effect Use Colors(NGUI打字效果加上颜色)

来源:互联网 发布:体育专业表格数据分析 编辑:程序博客网 时间:2024/06/05 04:29

using UnityEngine;/// <summary>/// Trivial script that fills the label's contents gradually, as if someone was typing./// </summary>[RequireComponent(typeof(UILabel))][AddComponentMenu("NGUI/Examples/Typewriter Effect")]public class TypewriterEffect : MonoBehaviour{public int charsPerSecond = 40;public AudioClip typeSound;UILabel mLabel;string mText;int mOffset = 0;float mNextChar = 0f;void OnEnable(){mLabel = GetComponent<UILabel>();mLabel.enabled = false;mLabel = null;} void Start(){}public void FinishTypewriter(){mLabel.text  = mText;Destroy(this);}void Update (){if (mLabel == null){mLabel = GetComponent<UILabel>();//mLabel.supportEncoding = false;mLabel.enabled = true;mLabel.symbolStyle = UIFont.SymbolStyle.None;mText = mLabel.font.WrapText(mLabel.text, mLabel.lineWidth / mLabel.cachedTransform.localScale.x, mLabel.maxLineCount, true, UIFont.SymbolStyle.None);}if (mOffset < mText.Length){if (mNextChar <= Time.time){charsPerSecond = Mathf.Max(1, charsPerSecond);// Periods and end-of-line characters should pause for a longer time.float delay = 1f / charsPerSecond;char c = mText[mOffset];if (c == '.' || c == ',' || c == '!' || c == '?' || c == ' ') delay *= 4f;else if (c == '。' || c == '?' || c == '!' || c == ',' ) delay *= 4f;string s = mText.Substring(0, ++mOffset);if(c == '['){for(int i = 0;i< 20; i++){s = mText.Substring(0, ++mOffset);int index2 = s.IndexOf(']',mOffset - 1);if(index2 != -1 ){break;}}}else if(c == '\\'){s = mText.Substring(0, ++mOffset);}mLabel.text  = s;if(typeSound != null){AudioManage.Instance.PlayOneShot(typeSound);}mNextChar = Time.time + delay;}}else Destroy(this);}}


原创粉丝点击