PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/Assets/NGUI/Scripts/Internal/ActiveAnimation.cs

https://gitlab.com/knnnrd/arbattlebot
C# | 352 lines | 241 code | 54 blank | 57 comment | 104 complexity | f586ede7eef54a54e22bbcf7d9e96162 MD5 | raw file
  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2014 Tasharen Entertainment
  4. //----------------------------------------------
  5. #if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2
  6. #define USE_MECANIM
  7. #endif
  8. using UnityEngine;
  9. using AnimationOrTween;
  10. using System.Collections.Generic;
  11. /// <summary>
  12. /// Mainly an internal script used by UIButtonPlayAnimation, but can also be used to call
  13. /// the specified function on the game object after it finishes animating.
  14. /// </summary>
  15. [AddComponentMenu("NGUI/Internal/Active Animation")]
  16. public class ActiveAnimation : MonoBehaviour
  17. {
  18. /// <summary>
  19. /// Active animation that resulted in the event notification.
  20. /// </summary>
  21. static public ActiveAnimation current;
  22. /// <summary>
  23. /// Event delegates called when the animation finishes.
  24. /// </summary>
  25. public List<EventDelegate> onFinished = new List<EventDelegate>();
  26. // Deprecated functionality, kept for backwards compatibility
  27. [HideInInspector] public GameObject eventReceiver;
  28. [HideInInspector] public string callWhenFinished;
  29. Animation mAnim;
  30. Direction mLastDirection = Direction.Toggle;
  31. Direction mDisableDirection = Direction.Toggle;
  32. bool mNotify = false;
  33. #if USE_MECANIM
  34. Animator mAnimator;
  35. string mClip = "";
  36. float playbackTime
  37. {
  38. get
  39. {
  40. AnimatorStateInfo state = mAnimator.GetCurrentAnimatorStateInfo(0);
  41. return Mathf.Clamp01(state.normalizedTime);
  42. }
  43. }
  44. #endif
  45. /// <summary>
  46. /// Whether the animation is currently playing.
  47. /// </summary>
  48. public bool isPlaying
  49. {
  50. get
  51. {
  52. if (mAnim == null)
  53. {
  54. #if USE_MECANIM
  55. if (mAnimator != null)
  56. {
  57. if (mLastDirection == Direction.Reverse)
  58. {
  59. if (playbackTime == 0f) return false;
  60. }
  61. else if (playbackTime == 1f) return false;
  62. return true;
  63. }
  64. #endif
  65. return false;
  66. }
  67. foreach (AnimationState state in mAnim)
  68. {
  69. if (!mAnim.IsPlaying(state.name)) continue;
  70. if (mLastDirection == Direction.Forward)
  71. {
  72. if (state.time < state.length) return true;
  73. }
  74. else if (mLastDirection == Direction.Reverse)
  75. {
  76. if (state.time > 0f) return true;
  77. }
  78. else return true;
  79. }
  80. return false;
  81. }
  82. }
  83. /// <summary>
  84. /// Manually reset the active animation to the beginning.
  85. /// </summary>
  86. public void Reset ()
  87. {
  88. if (mAnim != null)
  89. {
  90. foreach (AnimationState state in mAnim)
  91. {
  92. if (mLastDirection == Direction.Reverse) state.time = state.length;
  93. else if (mLastDirection == Direction.Forward) state.time = 0f;
  94. }
  95. }
  96. #if USE_MECANIM
  97. else if (mAnimator != null)
  98. {
  99. mAnimator.Play(mClip, 0, (mLastDirection == Direction.Reverse) ? 1f : 0f);
  100. }
  101. #endif
  102. }
  103. /// <summary>
  104. /// Event receiver is only kept for backwards compatibility purposes. It's removed on start if new functionality is used.
  105. /// </summary>
  106. void Start ()
  107. {
  108. if (eventReceiver != null && EventDelegate.IsValid(onFinished))
  109. {
  110. eventReceiver = null;
  111. callWhenFinished = null;
  112. }
  113. }
  114. /// <summary>
  115. /// Notify the target when the animation finishes playing.
  116. /// </summary>
  117. void Update ()
  118. {
  119. float delta = RealTime.deltaTime;
  120. if (delta == 0f) return;
  121. #if USE_MECANIM
  122. if (mAnimator != null)
  123. {
  124. mAnimator.Update((mLastDirection == Direction.Reverse) ? -delta : delta);
  125. if (isPlaying) return;
  126. mAnimator.enabled = false;
  127. enabled = false;
  128. }
  129. else if (mAnim != null)
  130. #else
  131. if (mAnim != null)
  132. #endif
  133. {
  134. bool playing = false;
  135. foreach (AnimationState state in mAnim)
  136. {
  137. if (!mAnim.IsPlaying(state.name)) continue;
  138. float movement = state.speed * delta;
  139. state.time += movement;
  140. if (movement < 0f)
  141. {
  142. if (state.time > 0f) playing = true;
  143. else state.time = 0f;
  144. }
  145. else
  146. {
  147. if (state.time < state.length) playing = true;
  148. else state.time = state.length;
  149. }
  150. }
  151. mAnim.Sample();
  152. if (playing) return;
  153. enabled = false;
  154. }
  155. else
  156. {
  157. enabled = false;
  158. return;
  159. }
  160. if (mNotify)
  161. {
  162. mNotify = false;
  163. current = this;
  164. EventDelegate.Execute(onFinished);
  165. // Deprecated functionality, kept for backwards compatibility
  166. if (eventReceiver != null && !string.IsNullOrEmpty(callWhenFinished))
  167. eventReceiver.SendMessage(callWhenFinished, SendMessageOptions.DontRequireReceiver);
  168. current = null;
  169. if (mDisableDirection != Direction.Toggle && mLastDirection == mDisableDirection)
  170. NGUITools.SetActive(gameObject, false);
  171. }
  172. }
  173. /// <summary>
  174. /// Play the specified animation.
  175. /// </summary>
  176. void Play (string clipName, Direction playDirection)
  177. {
  178. // Determine the play direction
  179. if (playDirection == Direction.Toggle)
  180. playDirection = (mLastDirection != Direction.Forward) ? Direction.Forward : Direction.Reverse;
  181. if (mAnim != null)
  182. {
  183. // We will sample the animation manually so that it works when the time is paused
  184. enabled = true;
  185. mAnim.enabled = false;
  186. bool noName = string.IsNullOrEmpty(clipName);
  187. // Play the animation if it's not playing already
  188. if (noName)
  189. {
  190. if (!mAnim.isPlaying) mAnim.Play();
  191. }
  192. else if (!mAnim.IsPlaying(clipName))
  193. {
  194. mAnim.Play(clipName);
  195. }
  196. // Update the animation speed based on direction -- forward or back
  197. foreach (AnimationState state in mAnim)
  198. {
  199. if (string.IsNullOrEmpty(clipName) || state.name == clipName)
  200. {
  201. float speed = Mathf.Abs(state.speed);
  202. state.speed = speed * (int)playDirection;
  203. // Automatically start the animation from the end if it's playing in reverse
  204. if (playDirection == Direction.Reverse && state.time == 0f) state.time = state.length;
  205. else if (playDirection == Direction.Forward && state.time == state.length) state.time = 0f;
  206. }
  207. }
  208. // Remember the direction for disable checks in Update()
  209. mLastDirection = playDirection;
  210. mNotify = true;
  211. mAnim.Sample();
  212. }
  213. #if USE_MECANIM
  214. else if (mAnimator != null)
  215. {
  216. if (enabled && isPlaying)
  217. {
  218. if (mClip == clipName)
  219. {
  220. mLastDirection = playDirection;
  221. return;
  222. }
  223. }
  224. enabled = true;
  225. mNotify = true;
  226. mLastDirection = playDirection;
  227. mClip = clipName;
  228. mAnimator.Play(mClip, 0, (playDirection == Direction.Forward) ? 0f : 1f);
  229. // NOTE: If you are getting a message "Animator.GotoState: HealthState could not be found"
  230. // it means that you chose a state name that doesn't exist in the Animator window.
  231. }
  232. #endif
  233. }
  234. /// <summary>
  235. /// Play the specified animation on the specified object.
  236. /// </summary>
  237. static public ActiveAnimation Play (Animation anim, string clipName, Direction playDirection,
  238. EnableCondition enableBeforePlay, DisableCondition disableCondition)
  239. {
  240. if (!NGUITools.GetActive(anim.gameObject))
  241. {
  242. // If the object is disabled, don't do anything
  243. if (enableBeforePlay != EnableCondition.EnableThenPlay) return null;
  244. // Enable the game object before animating it
  245. NGUITools.SetActive(anim.gameObject, true);
  246. // Refresh all panels right away so that there is no one frame delay
  247. UIPanel[] panels = anim.gameObject.GetComponentsInChildren<UIPanel>();
  248. for (int i = 0, imax = panels.Length; i < imax; ++i) panels[i].Refresh();
  249. }
  250. ActiveAnimation aa = anim.GetComponent<ActiveAnimation>();
  251. if (aa == null) aa = anim.gameObject.AddComponent<ActiveAnimation>();
  252. aa.mAnim = anim;
  253. aa.mDisableDirection = (Direction)(int)disableCondition;
  254. aa.onFinished.Clear();
  255. aa.Play(clipName, playDirection);
  256. return aa;
  257. }
  258. /// <summary>
  259. /// Play the specified animation.
  260. /// </summary>
  261. static public ActiveAnimation Play (Animation anim, string clipName, Direction playDirection)
  262. {
  263. return Play(anim, clipName, playDirection, EnableCondition.DoNothing, DisableCondition.DoNotDisable);
  264. }
  265. /// <summary>
  266. /// Play the specified animation.
  267. /// </summary>
  268. static public ActiveAnimation Play (Animation anim, Direction playDirection)
  269. {
  270. return Play(anim, null, playDirection, EnableCondition.DoNothing, DisableCondition.DoNotDisable);
  271. }
  272. #if USE_MECANIM
  273. /// <summary>
  274. /// Play the specified animation on the specified object.
  275. /// </summary>
  276. static public ActiveAnimation Play (Animator anim, string clipName, Direction playDirection,
  277. EnableCondition enableBeforePlay, DisableCondition disableCondition)
  278. {
  279. if (!NGUITools.GetActive(anim.gameObject))
  280. {
  281. // If the object is disabled, don't do anything
  282. if (enableBeforePlay != EnableCondition.EnableThenPlay) return null;
  283. // Enable the game object before animating it
  284. NGUITools.SetActive(anim.gameObject, true);
  285. // Refresh all panels right away so that there is no one frame delay
  286. UIPanel[] panels = anim.gameObject.GetComponentsInChildren<UIPanel>();
  287. for (int i = 0, imax = panels.Length; i < imax; ++i) panels[i].Refresh();
  288. }
  289. ActiveAnimation aa = anim.GetComponent<ActiveAnimation>();
  290. if (aa == null) aa = anim.gameObject.AddComponent<ActiveAnimation>();
  291. aa.mAnimator = anim;
  292. aa.mDisableDirection = (Direction)(int)disableCondition;
  293. aa.onFinished.Clear();
  294. aa.Play(clipName, playDirection);
  295. return aa;
  296. }
  297. #endif
  298. }