Guest User

Untitled

a guest
Oct 20th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. //-------------------------------------------------------
  2. // @file CommonTextAnimation.cs
  3. // @brief IMeshModifierを利用したテキストアニメーション.
  4. //
  5. // @author haruki.tachihara
  6. //-------------------------------------------------------
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityEngine.EventSystems;
  11.  
  12. //これは別UnityEngine名前空間を使ってはいるが自前のTweenライブラリ
  13. using Method = UnityEngine.UI.TweenMain.Method;
  14.  
  15. namespace UnityEngine.UI
  16. {
  17. [RequireComponent(typeof(Text))]
  18. [DisallowMultipleComponent]
  19. public class CommonTextAnimation : BaseMeshEffect
  20. {
  21. // 1文字は6頂点.
  22. const int CHAR_VERT_CNT = 6;
  23.  
  24. #region Animation Parameter
  25.  
  26. // OnEnableで自動開始するか.
  27. public bool autoStartOnEnable = true;
  28. // アニメーションにかける所用時間 [秒]
  29. public float duration = 0.5f;
  30. // X軸の動きの大きさ.
  31. public float moveAmountX = 0f;
  32. // Y軸の動きの大きさ.
  33. public float moveAmountY = 0f;
  34. // シェイクは有効か.
  35. public bool enableShake = true;
  36. // シェイク量.
  37. public float shakeAmount = 1f;
  38.  
  39. #endregion
  40.  
  41. private bool isFinished;
  42. private float time;
  43.  
  44. public void StartEffect()
  45. {
  46. time = 0;
  47. isFinished = false;
  48. }
  49.  
  50. protected override void OnEnable()
  51. {
  52. base.OnEnable();
  53. if (autoStartOnEnable)
  54. {
  55. StartEffect();
  56. }
  57. }
  58.  
  59. protected virtual void Update()
  60. {
  61. if (isFinished)
  62. return;
  63.  
  64. time += Time.deltaTime;
  65. if (graphic != null)
  66. {
  67. graphic.SetVerticesDirty();
  68. }
  69. }
  70.  
  71. public override void ModifyMesh(VertexHelper vh)
  72. {
  73. var vList = new List<UIVertex>();
  74. vh.GetUIVertexStream(vList);
  75.  
  76. Modify(ref vList);
  77.  
  78. vh.Clear();
  79. vh.AddUIVertexTriangleStream(vList);
  80. }
  81.  
  82. void Modify(ref List<UIVertex> vList)
  83. {
  84.  
  85. if (isFinished || duration <= 0f || vList.Count < CHAR_VERT_CNT)
  86. {
  87. return;
  88. }
  89. if (time >= duration && !enableShake)
  90. {
  91. isFinished = true;
  92. }
  93.  
  94. var vertexCount = vList.Count;
  95. var charCount = vertexCount / CHAR_VERT_CNT;
  96. var deltaTimeParChar = duration / charCount;
  97. Vector3 shake = Vector3.zero;
  98.  
  99. if (enableShake)
  100. {
  101. shake = Random.insideUnitCircle * shakeAmount;
  102. }
  103.  
  104. for (int i = 0; i < vertexCount; i += CHAR_VERT_CNT)
  105. {
  106. // 現在の文字のnormalizeTime[0-1]を計算.
  107. var startTime = i / CHAR_VERT_CNT * deltaTimeParChar;
  108. var elapsedTime = time - startTime;
  109. var normalizeTime = Mathf.Clamp01(elapsedTime / deltaTimeParChar);
  110.  
  111. if (normalizeTime <= 0f)
  112. {
  113. // 見えないように設定.
  114. for (int vertexIndex = 0; vertexIndex < CHAR_VERT_CNT; vertexIndex++)
  115. {
  116. var v = vList[i + vertexIndex];
  117. v.color = Color.clear;
  118. vList[i + vertexIndex] = v;
  119. }
  120. }
  121. else if (normalizeTime >= 1f)
  122. {
  123. if (enableShake)
  124. {
  125. for (int vertexIndex = 0; vertexIndex < CHAR_VERT_CNT; vertexIndex++)
  126. {
  127. var v = vList[i + vertexIndex];
  128. v.position += shake;
  129. vList[i + vertexIndex] = v;
  130. }
  131. }
  132. }
  133. else
  134. {
  135. // アニメーションさせる.
  136. var center = Vector2.Lerp(vList[i].position, vList[i + 3].position, 0.5f);
  137.  
  138. for (int vertexIndex = 0; vertexIndex < CHAR_VERT_CNT; vertexIndex++)
  139. {
  140. var v = vList[i + vertexIndex];
  141. var sampled = Tweening.Sample(Method.EaseOut, normalizeTime);
  142.  
  143. // 移動量
  144. var moveX = moveAmountX * Tweening.Sample(Method.EaseIn, 1f - normalizeTime);
  145. var moveY = moveAmountY * Mathf.Sin((1f - normalizeTime) * Mathf.PI);//上下にサインカーブ移動.
  146.  
  147. // スケール
  148. var posX = Mathf.Lerp(center.x, v.position.x, sampled) + moveX;
  149. var posY = Mathf.Lerp(center.y, v.position.y, sampled) + moveY;
  150.  
  151. v.position = new Vector2(posX + shake.x, posY + shake.y);
  152.  
  153. vList[i + vertexIndex] = v;
  154. }
  155. }
  156. }
  157. }
  158. }
  159.  
  160. public static class Tweening
  161. {
  162. public static float Sample(Method method, float normalizeTime)
  163. {
  164. float val = Mathf.Clamp01(normalizeTime);
  165.  
  166. switch (method)
  167. {
  168. case Method.Linear:
  169. val = Easing.Linear(val);
  170. break;
  171.  
  172. case Method.EaseIn:
  173. val = Easing.Sinusoidal.In(val);
  174. break;
  175.  
  176. case Method.EaseOut:
  177. val = Easing.Sinusoidal.Out(val);
  178. break;
  179.  
  180. case Method.EaseInOut:
  181. val = Easing.Sinusoidal.InOut(val);
  182. break;
  183.  
  184. case Method.BounceIn:
  185. val = Easing.Bounce.In(val);
  186. break;
  187.  
  188. case Method.BounceOut:
  189. val = Easing.Bounce.Out(val);
  190. break;
  191.  
  192. case Method.BounceInOut:
  193. val = Easing.Bounce.InOut(val);
  194. break;
  195. }
  196. return val;
  197. }
  198. }
  199. }
Add Comment
Please, Sign In to add comment