123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- [RequireComponent(typeof(UILabel))]
- [AddComponentMenu("NGUI/Interaction/Typewriter Effect")]
- public class TypewriterEffect : MonoBehaviour
- {
- public bool isActive
- {
- get
- {
- return this.mActive;
- }
- }
- public void ResetToBeginning()
- {
- this.Finish();
- this.mReset = true;
- this.mActive = true;
- this.mNextChar = 0f;
- this.mCurrentOffset = 0;
- this.Update();
- }
- public void Finish()
- {
- if (this.mActive)
- {
- this.mActive = false;
- if (!this.mReset)
- {
- this.mCurrentOffset = this.mFullText.Length;
- this.mFade.Clear();
- this.mLabel.text = this.mFullText;
- }
- if (this.keepFullDimensions && this.scrollView != null)
- {
- this.scrollView.UpdatePosition();
- }
- TypewriterEffect.current = this;
- EventDelegate.Execute(this.onFinished);
- TypewriterEffect.current = null;
- }
- }
- private void OnEnable()
- {
- this.mReset = true;
- this.mActive = true;
- }
- private void Update()
- {
- if (!this.mActive)
- {
- return;
- }
- if (this.mReset)
- {
- this.mCurrentOffset = 0;
- this.mReset = false;
- this.mLabel = base.GetComponent<UILabel>();
- this.mFullText = this.mLabel.processedText;
- this.mFade.Clear();
- if (this.keepFullDimensions && this.scrollView != null)
- {
- this.scrollView.UpdatePosition();
- }
- }
- while (this.mCurrentOffset < this.mFullText.Length && this.mNextChar <= RealTime.time)
- {
- int num = this.mCurrentOffset;
- this.charsPerSecond = Mathf.Max(1, this.charsPerSecond);
- while (NGUIText.ParseSymbol(this.mFullText, ref this.mCurrentOffset))
- {
- }
- this.mCurrentOffset++;
- if (this.mCurrentOffset > this.mFullText.Length)
- {
- break;
- }
- float num2 = 1f / (float)this.charsPerSecond;
- char c = (num >= this.mFullText.Length) ? '\n' : this.mFullText[num];
- if (c == '\n')
- {
- num2 += this.delayOnNewLine;
- }
- else if (num + 1 == this.mFullText.Length || this.mFullText[num + 1] <= ' ')
- {
- if (c == '.')
- {
- if (num + 2 < this.mFullText.Length && this.mFullText[num + 1] == '.' && this.mFullText[num + 2] == '.')
- {
- num2 += this.delayOnPeriod * 3f;
- num += 2;
- }
- else
- {
- num2 += this.delayOnPeriod;
- }
- }
- else if (c == '!' || c == '?')
- {
- num2 += this.delayOnPeriod;
- }
- }
- if (this.mNextChar == 0f)
- {
- this.mNextChar = RealTime.time + num2;
- }
- else
- {
- this.mNextChar += num2;
- }
- if (this.fadeInTime != 0f)
- {
- TypewriterEffect.FadeEntry item = default(TypewriterEffect.FadeEntry);
- item.index = num;
- item.alpha = 0f;
- item.text = this.mFullText.Substring(num, this.mCurrentOffset - num);
- this.mFade.Add(item);
- }
- else
- {
- this.mLabel.text = ((!this.keepFullDimensions) ? this.mFullText.Substring(0, this.mCurrentOffset) : (this.mFullText.Substring(0, this.mCurrentOffset) + "[00]" + this.mFullText.Substring(this.mCurrentOffset)));
- if (!this.keepFullDimensions && this.scrollView != null)
- {
- this.scrollView.UpdatePosition();
- }
- }
- }
- if (this.mFade.size != 0)
- {
- int i = 0;
- while (i < this.mFade.size)
- {
- TypewriterEffect.FadeEntry value = this.mFade[i];
- value.alpha += RealTime.deltaTime / this.fadeInTime;
- if (value.alpha < 1f)
- {
- this.mFade[i] = value;
- i++;
- }
- else
- {
- this.mFade.RemoveAt(i);
- }
- }
- if (this.mFade.size == 0)
- {
- if (this.keepFullDimensions)
- {
- this.mLabel.text = this.mFullText.Substring(0, this.mCurrentOffset) + "[00]" + this.mFullText.Substring(this.mCurrentOffset);
- }
- else
- {
- this.mLabel.text = this.mFullText.Substring(0, this.mCurrentOffset);
- }
- }
- else
- {
- StringBuilder stringBuilder = new StringBuilder();
- for (int j = 0; j < this.mFade.size; j++)
- {
- TypewriterEffect.FadeEntry fadeEntry = this.mFade[j];
- if (j == 0)
- {
- stringBuilder.Append(this.mFullText.Substring(0, fadeEntry.index));
- }
- stringBuilder.Append('[');
- stringBuilder.Append(NGUIText.EncodeAlpha(fadeEntry.alpha));
- stringBuilder.Append(']');
- stringBuilder.Append(fadeEntry.text);
- }
- if (this.keepFullDimensions)
- {
- stringBuilder.Append("[00]");
- stringBuilder.Append(this.mFullText.Substring(this.mCurrentOffset));
- }
- this.mLabel.text = stringBuilder.ToString();
- }
- }
- else if (this.mCurrentOffset == this.mFullText.Length)
- {
- TypewriterEffect.current = this;
- EventDelegate.Execute(this.onFinished);
- TypewriterEffect.current = null;
- this.mActive = false;
- }
- }
- public static TypewriterEffect current;
- public int charsPerSecond = 20;
- public float fadeInTime;
- public float delayOnPeriod;
- public float delayOnNewLine;
- public UIScrollView scrollView;
- public bool keepFullDimensions;
- public List<EventDelegate> onFinished = new List<EventDelegate>();
- private UILabel mLabel;
- private string mFullText = string.Empty;
- private int mCurrentOffset;
- private float mNextChar;
- private bool mReset = true;
- private bool mActive;
- private BetterList<TypewriterEffect.FadeEntry> mFade = new BetterList<TypewriterEffect.FadeEntry>();
- private struct FadeEntry
- {
- public int index;
- public string text;
- public float alpha;
- }
- }
|