TypewriterEffect.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. [RequireComponent(typeof(UILabel))]
  6. [AddComponentMenu("NGUI/Interaction/Typewriter Effect")]
  7. public class TypewriterEffect : MonoBehaviour
  8. {
  9. public bool isActive
  10. {
  11. get
  12. {
  13. return this.mActive;
  14. }
  15. }
  16. public void ResetToBeginning()
  17. {
  18. this.Finish();
  19. this.mReset = true;
  20. this.mActive = true;
  21. this.mNextChar = 0f;
  22. this.mCurrentOffset = 0;
  23. this.Update();
  24. }
  25. public void Finish()
  26. {
  27. if (this.mActive)
  28. {
  29. this.mActive = false;
  30. if (!this.mReset)
  31. {
  32. this.mCurrentOffset = this.mFullText.Length;
  33. this.mFade.Clear();
  34. this.mLabel.text = this.mFullText;
  35. }
  36. if (this.keepFullDimensions && this.scrollView != null)
  37. {
  38. this.scrollView.UpdatePosition();
  39. }
  40. TypewriterEffect.current = this;
  41. EventDelegate.Execute(this.onFinished);
  42. TypewriterEffect.current = null;
  43. }
  44. }
  45. private void OnEnable()
  46. {
  47. this.mReset = true;
  48. this.mActive = true;
  49. }
  50. private void Update()
  51. {
  52. if (!this.mActive)
  53. {
  54. return;
  55. }
  56. if (this.mReset)
  57. {
  58. this.mCurrentOffset = 0;
  59. this.mReset = false;
  60. this.mLabel = base.GetComponent<UILabel>();
  61. this.mFullText = this.mLabel.processedText;
  62. this.mFade.Clear();
  63. if (this.keepFullDimensions && this.scrollView != null)
  64. {
  65. this.scrollView.UpdatePosition();
  66. }
  67. }
  68. while (this.mCurrentOffset < this.mFullText.Length && this.mNextChar <= RealTime.time)
  69. {
  70. int num = this.mCurrentOffset;
  71. this.charsPerSecond = Mathf.Max(1, this.charsPerSecond);
  72. while (NGUIText.ParseSymbol(this.mFullText, ref this.mCurrentOffset))
  73. {
  74. }
  75. this.mCurrentOffset++;
  76. if (this.mCurrentOffset > this.mFullText.Length)
  77. {
  78. break;
  79. }
  80. float num2 = 1f / (float)this.charsPerSecond;
  81. char c = (num >= this.mFullText.Length) ? '\n' : this.mFullText[num];
  82. if (c == '\n')
  83. {
  84. num2 += this.delayOnNewLine;
  85. }
  86. else if (num + 1 == this.mFullText.Length || this.mFullText[num + 1] <= ' ')
  87. {
  88. if (c == '.')
  89. {
  90. if (num + 2 < this.mFullText.Length && this.mFullText[num + 1] == '.' && this.mFullText[num + 2] == '.')
  91. {
  92. num2 += this.delayOnPeriod * 3f;
  93. num += 2;
  94. }
  95. else
  96. {
  97. num2 += this.delayOnPeriod;
  98. }
  99. }
  100. else if (c == '!' || c == '?')
  101. {
  102. num2 += this.delayOnPeriod;
  103. }
  104. }
  105. if (this.mNextChar == 0f)
  106. {
  107. this.mNextChar = RealTime.time + num2;
  108. }
  109. else
  110. {
  111. this.mNextChar += num2;
  112. }
  113. if (this.fadeInTime != 0f)
  114. {
  115. TypewriterEffect.FadeEntry item = default(TypewriterEffect.FadeEntry);
  116. item.index = num;
  117. item.alpha = 0f;
  118. item.text = this.mFullText.Substring(num, this.mCurrentOffset - num);
  119. this.mFade.Add(item);
  120. }
  121. else
  122. {
  123. this.mLabel.text = ((!this.keepFullDimensions) ? this.mFullText.Substring(0, this.mCurrentOffset) : (this.mFullText.Substring(0, this.mCurrentOffset) + "[00]" + this.mFullText.Substring(this.mCurrentOffset)));
  124. if (!this.keepFullDimensions && this.scrollView != null)
  125. {
  126. this.scrollView.UpdatePosition();
  127. }
  128. }
  129. }
  130. if (this.mFade.size != 0)
  131. {
  132. int i = 0;
  133. while (i < this.mFade.size)
  134. {
  135. TypewriterEffect.FadeEntry value = this.mFade[i];
  136. value.alpha += RealTime.deltaTime / this.fadeInTime;
  137. if (value.alpha < 1f)
  138. {
  139. this.mFade[i] = value;
  140. i++;
  141. }
  142. else
  143. {
  144. this.mFade.RemoveAt(i);
  145. }
  146. }
  147. if (this.mFade.size == 0)
  148. {
  149. if (this.keepFullDimensions)
  150. {
  151. this.mLabel.text = this.mFullText.Substring(0, this.mCurrentOffset) + "[00]" + this.mFullText.Substring(this.mCurrentOffset);
  152. }
  153. else
  154. {
  155. this.mLabel.text = this.mFullText.Substring(0, this.mCurrentOffset);
  156. }
  157. }
  158. else
  159. {
  160. StringBuilder stringBuilder = new StringBuilder();
  161. for (int j = 0; j < this.mFade.size; j++)
  162. {
  163. TypewriterEffect.FadeEntry fadeEntry = this.mFade[j];
  164. if (j == 0)
  165. {
  166. stringBuilder.Append(this.mFullText.Substring(0, fadeEntry.index));
  167. }
  168. stringBuilder.Append('[');
  169. stringBuilder.Append(NGUIText.EncodeAlpha(fadeEntry.alpha));
  170. stringBuilder.Append(']');
  171. stringBuilder.Append(fadeEntry.text);
  172. }
  173. if (this.keepFullDimensions)
  174. {
  175. stringBuilder.Append("[00]");
  176. stringBuilder.Append(this.mFullText.Substring(this.mCurrentOffset));
  177. }
  178. this.mLabel.text = stringBuilder.ToString();
  179. }
  180. }
  181. else if (this.mCurrentOffset == this.mFullText.Length)
  182. {
  183. TypewriterEffect.current = this;
  184. EventDelegate.Execute(this.onFinished);
  185. TypewriterEffect.current = null;
  186. this.mActive = false;
  187. }
  188. }
  189. public static TypewriterEffect current;
  190. public int charsPerSecond = 20;
  191. public float fadeInTime;
  192. public float delayOnPeriod;
  193. public float delayOnNewLine;
  194. public UIScrollView scrollView;
  195. public bool keepFullDimensions;
  196. public List<EventDelegate> onFinished = new List<EventDelegate>();
  197. private UILabel mLabel;
  198. private string mFullText = string.Empty;
  199. private int mCurrentOffset;
  200. private float mNextChar;
  201. private bool mReset = true;
  202. private bool mActive;
  203. private BetterList<TypewriterEffect.FadeEntry> mFade = new BetterList<TypewriterEffect.FadeEntry>();
  204. private struct FadeEntry
  205. {
  206. public int index;
  207. public string text;
  208. public float alpha;
  209. }
  210. }