項目中使用NGUI時用到了打字效果,NGUI自帶有這樣的腳本,效果還是不錯的。按要求設定,一切正常。
但是腳本的使用并不是用完就OK,可能會有多次使用,每次都要求這種效果,也就是同一個腳本,多次使用。一般來說,隻要下次使用前reset恢複到原始參數就行了。
但是問題來了,運作第一次是正常的,後面老是莫名其妙的沒打字效果,api方面能用的函數就ResetToBeginning和Finish兩個函數,看源碼Finish不是,那就是ResetToBeginning的問題了,可能有寫東西沒重置成功。
斷點,代碼正常執行,斷點檢視打字效果也是有的。
再次檢視,發現問題,每幀運作都更改了字元長度,難道是打字速度問題。調整參數,無效。
再次斷點,兩個判斷條件 (mCurrentOffset < mFullText.Length && mNextChar <= RealTime.time),
第一個成立很正常,問題就隻能出現在第二個條件了,mNextChar <= RealTime.time,第一遍正常,後面有問題,斷點檢視mNextChar 的參數很容易查出mNextChar 沒有在reset是重置。
問題找出來了,解決辦法就是在update的重置區裡面加上對應的重置語句就行了。mNextChar = 0;
完整代碼
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2014 Tasharen Entertainment
//----------------------------------------------
using UnityEngine;
using System.Text;
using System.Collections.Generic;
/// <summary>
/// This script is able to fill in the label's text gradually, giving the effect of someone typing or fading in the content over time.
/// </summary>
[RequireComponent(typeof(UILabel))]
[AddComponentMenu("NGUI/Interaction/Typewriter Effect")]
public class TypewriterEffect : MonoBehaviour
{
static public TypewriterEffect current;
struct FadeEntry
{
public int index;
public string text;
public float alpha;
}
/// <summary>
/// How many characters will be printed per second.
/// </summary>
public int charsPerSecond = ;
/// <summary>
/// How long it takes for each character to fade in.
/// </summary>
public float fadeInTime = f;
/// <summary>
/// How long to pause when a period is encountered (in seconds).
/// </summary>
public float delayOnPeriod = f;
/// <summary>
/// How long to pause when a new line character is encountered (in seconds).
/// </summary>
public float delayOnNewLine = f;
/// <summary>
/// If a scroll view is specified, its UpdatePosition() function will be called every time the text is updated.
/// </summary>
public UIScrollView scrollView;
/// <summary>
/// If set to 'true', the label's dimensions will be that of a fully faded-in content.
/// </summary>
public bool keepFullDimensions = false;
/// <summary>
/// Event delegate triggered when the typewriter effect finishes.
/// </summary>
public List<EventDelegate> onFinished = new List<EventDelegate>();
UILabel mLabel;
string mFullText = "";
int mCurrentOffset = ;
float mNextChar = f;
bool mReset = true;
bool mActive = false;
BetterList<FadeEntry> mFade = new BetterList<FadeEntry>();
/// <summary>
/// Whether the typewriter effect is currently active or not.
/// </summary>
public bool isActive { get { return mActive; } }
/// <summary>
/// Reset the typewriter effect to the beginning of the label.
/// </summary>
public void ResetToBeginning ()
{
Finish();
mReset = true;
mActive = true;
}
/// <summary>
/// Finish the typewriter operation and show all the text right away.
/// </summary>
public void Finish ()
{
if (mActive)
{
mActive = false;
if (!mReset)
{
mCurrentOffset = mFullText.Length;
mFade.Clear();
mLabel.text = mFullText;
}
if (keepFullDimensions && scrollView != null)
scrollView.UpdatePosition();
current = this;
EventDelegate.Execute(onFinished);
current = null;
}
}
void OnEnable () { mReset = true; mActive = true; }
void Update ()
{
if (!mActive) return;
if (mReset)
{
mCurrentOffset = ;
mReset = false;
mLabel = GetComponent<UILabel>();
mFullText = mLabel.processedText;
mFade.Clear();
mNextChar = ;
if (keepFullDimensions && scrollView != null) scrollView.UpdatePosition();
}
while (mCurrentOffset < mFullText.Length && mNextChar <= RealTime.time)
{
int lastOffset = mCurrentOffset;
charsPerSecond = Mathf.Max(, charsPerSecond);
// Automatically skip all symbols
while (NGUIText.ParseSymbol(mFullText, ref mCurrentOffset)) { }
++mCurrentOffset;
// Periods and end-of-line characters should pause for a longer time.
float delay = f / charsPerSecond;
char c = (lastOffset < mFullText.Length) ? mFullText[lastOffset] : '\n';
if (c == '\n')
{
delay += delayOnNewLine;
}
else if (lastOffset + == mFullText.Length || mFullText[lastOffset + ] <= ' ')
{
if (c == '.')
{
if (lastOffset + < mFullText.Length && mFullText[lastOffset + ] == '.' && mFullText[lastOffset + ] == '.')
{
delay += delayOnPeriod * f;
lastOffset += ;
}
else delay += delayOnPeriod;
}
else if (c == '!' || c == '?')
{
delay += delayOnPeriod;
}
}
if (mNextChar == f)
{
mNextChar = RealTime.time + delay;
}
else mNextChar += delay;
if (fadeInTime != f)
{
// There is smooth fading involved
FadeEntry fe = new FadeEntry();
fe.index = lastOffset;
fe.alpha = f;
fe.text = mFullText.Substring(lastOffset, mCurrentOffset - lastOffset);
mFade.Add(fe);
}
else
{
// No smooth fading necessary
mLabel.text = keepFullDimensions ?
mFullText.Substring(, mCurrentOffset) + "[00]" + mFullText.Substring(mCurrentOffset) :
mFullText.Substring(, mCurrentOffset);
// If a scroll view was specified, update its position
if (!keepFullDimensions && scrollView != null) scrollView.UpdatePosition();
}
}
// Alpha-based fading
if (mFade.size != )
{
for (int i = ; i < mFade.size; )
{
FadeEntry fe = mFade[i];
fe.alpha += RealTime.deltaTime / fadeInTime;
if (fe.alpha < f)
{
mFade[i] = fe;
++i;
}
else mFade.RemoveAt(i);
}
if (mFade.size == )
{
if (keepFullDimensions) mLabel.text = mFullText.Substring(, mCurrentOffset) + "[00]" + mFullText.Substring(mCurrentOffset);
else mLabel.text = mFullText.Substring(, mCurrentOffset);
}
else
{
StringBuilder sb = new StringBuilder();
for (int i = ; i < mFade.size; ++i)
{
FadeEntry fe = mFade[i];
if (i == )
{
sb.Append(mFullText.Substring(, fe.index));
}
sb.Append('[');
sb.Append(NGUIText.EncodeAlpha(fe.alpha));
sb.Append(']');
sb.Append(fe.text);
}
if (keepFullDimensions)
{
sb.Append("[00]");
sb.Append(mFullText.Substring(mCurrentOffset));
}
mLabel.text = sb.ToString();
}
}
else if (mCurrentOffset == mFullText.Length)
{
current = this;
EventDelegate.Execute(onFinished);
current = null;
mActive = false;
}
}
}