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);
}
}
The Text Typewriter Effect Use Colors(NGUI打字效果加上颜色)
最新推荐文章于 2025-02-17 09:59:19 发布
