您的位置:首页 > 产品设计 > UI/UE

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

2013-07-12 14:39 477 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: