ActiveAnimation.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using AnimationOrTween;
  5. using UnityEngine;
  6. [AddComponentMenu("NGUI/Internal/Active Animation")]
  7. public class ActiveAnimation : MonoBehaviour
  8. {
  9. private float playbackTime
  10. {
  11. get
  12. {
  13. return Mathf.Clamp01(this.mAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime);
  14. }
  15. }
  16. public bool isPlaying
  17. {
  18. get
  19. {
  20. if (!(this.mAnim == null))
  21. {
  22. IEnumerator enumerator = this.mAnim.GetEnumerator();
  23. try
  24. {
  25. while (enumerator.MoveNext())
  26. {
  27. object obj = enumerator.Current;
  28. AnimationState animationState = (AnimationState)obj;
  29. if (this.mAnim.IsPlaying(animationState.name))
  30. {
  31. if (this.mLastDirection == Direction.Forward)
  32. {
  33. if (animationState.time < animationState.length)
  34. {
  35. return true;
  36. }
  37. }
  38. else
  39. {
  40. if (this.mLastDirection != Direction.Reverse)
  41. {
  42. return true;
  43. }
  44. if (animationState.time > 0f)
  45. {
  46. return true;
  47. }
  48. }
  49. }
  50. }
  51. }
  52. finally
  53. {
  54. IDisposable disposable;
  55. if ((disposable = (enumerator as IDisposable)) != null)
  56. {
  57. disposable.Dispose();
  58. }
  59. }
  60. return false;
  61. }
  62. if (this.mAnimator != null)
  63. {
  64. if (this.mLastDirection == Direction.Reverse)
  65. {
  66. if (this.playbackTime == 0f)
  67. {
  68. return false;
  69. }
  70. }
  71. else if (this.playbackTime == 1f)
  72. {
  73. return false;
  74. }
  75. return true;
  76. }
  77. return false;
  78. }
  79. }
  80. public void Finish()
  81. {
  82. if (this.mAnim != null)
  83. {
  84. IEnumerator enumerator = this.mAnim.GetEnumerator();
  85. try
  86. {
  87. while (enumerator.MoveNext())
  88. {
  89. object obj = enumerator.Current;
  90. AnimationState animationState = (AnimationState)obj;
  91. if (this.mLastDirection == Direction.Forward)
  92. {
  93. animationState.time = animationState.length;
  94. }
  95. else if (this.mLastDirection == Direction.Reverse)
  96. {
  97. animationState.time = 0f;
  98. }
  99. }
  100. }
  101. finally
  102. {
  103. IDisposable disposable;
  104. if ((disposable = (enumerator as IDisposable)) != null)
  105. {
  106. disposable.Dispose();
  107. }
  108. }
  109. this.mAnim.Sample();
  110. }
  111. else if (this.mAnimator != null)
  112. {
  113. this.mAnimator.Play(this.mClip, 0, (this.mLastDirection != Direction.Forward) ? 0f : 1f);
  114. }
  115. }
  116. public void Reset()
  117. {
  118. if (this.mAnim != null)
  119. {
  120. IEnumerator enumerator = this.mAnim.GetEnumerator();
  121. try
  122. {
  123. while (enumerator.MoveNext())
  124. {
  125. object obj = enumerator.Current;
  126. AnimationState animationState = (AnimationState)obj;
  127. if (this.mLastDirection == Direction.Reverse)
  128. {
  129. animationState.time = animationState.length;
  130. }
  131. else if (this.mLastDirection == Direction.Forward)
  132. {
  133. animationState.time = 0f;
  134. }
  135. }
  136. }
  137. finally
  138. {
  139. IDisposable disposable;
  140. if ((disposable = (enumerator as IDisposable)) != null)
  141. {
  142. disposable.Dispose();
  143. }
  144. }
  145. }
  146. else if (this.mAnimator != null)
  147. {
  148. this.mAnimator.Play(this.mClip, 0, (this.mLastDirection != Direction.Reverse) ? 0f : 1f);
  149. }
  150. }
  151. private void Start()
  152. {
  153. if (this.eventReceiver != null && EventDelegate.IsValid(this.onFinished))
  154. {
  155. this.eventReceiver = null;
  156. this.callWhenFinished = null;
  157. }
  158. }
  159. private void Update()
  160. {
  161. float deltaTime = RealTime.deltaTime;
  162. if (deltaTime == 0f)
  163. {
  164. return;
  165. }
  166. if (this.mAnimator != null)
  167. {
  168. this.mAnimator.Update((this.mLastDirection != Direction.Reverse) ? deltaTime : (-deltaTime));
  169. if (this.isPlaying)
  170. {
  171. return;
  172. }
  173. this.mAnimator.enabled = false;
  174. base.enabled = false;
  175. }
  176. else
  177. {
  178. if (!(this.mAnim != null))
  179. {
  180. base.enabled = false;
  181. return;
  182. }
  183. bool flag = false;
  184. IEnumerator enumerator = this.mAnim.GetEnumerator();
  185. try
  186. {
  187. while (enumerator.MoveNext())
  188. {
  189. object obj = enumerator.Current;
  190. AnimationState animationState = (AnimationState)obj;
  191. if (this.mAnim.IsPlaying(animationState.name))
  192. {
  193. float num = animationState.speed * deltaTime;
  194. animationState.time += num;
  195. if (num < 0f)
  196. {
  197. if (animationState.time > 0f)
  198. {
  199. flag = true;
  200. }
  201. else
  202. {
  203. animationState.time = 0f;
  204. }
  205. }
  206. else if (animationState.time < animationState.length)
  207. {
  208. flag = true;
  209. }
  210. else
  211. {
  212. animationState.time = animationState.length;
  213. }
  214. }
  215. }
  216. }
  217. finally
  218. {
  219. IDisposable disposable;
  220. if ((disposable = (enumerator as IDisposable)) != null)
  221. {
  222. disposable.Dispose();
  223. }
  224. }
  225. this.mAnim.Sample();
  226. if (flag)
  227. {
  228. return;
  229. }
  230. base.enabled = false;
  231. }
  232. if (this.mNotify)
  233. {
  234. this.mNotify = false;
  235. if (ActiveAnimation.current == null)
  236. {
  237. ActiveAnimation.current = this;
  238. EventDelegate.Execute(this.onFinished);
  239. if (this.eventReceiver != null && !string.IsNullOrEmpty(this.callWhenFinished))
  240. {
  241. this.eventReceiver.SendMessage(this.callWhenFinished, SendMessageOptions.DontRequireReceiver);
  242. }
  243. ActiveAnimation.current = null;
  244. }
  245. if (this.mDisableDirection != Direction.Toggle && this.mLastDirection == this.mDisableDirection)
  246. {
  247. NGUITools.SetActive(base.gameObject, false);
  248. }
  249. }
  250. }
  251. private void Play(string clipName, Direction playDirection)
  252. {
  253. if (playDirection == Direction.Toggle)
  254. {
  255. playDirection = ((this.mLastDirection == Direction.Forward) ? Direction.Reverse : Direction.Forward);
  256. }
  257. if (this.mAnim != null)
  258. {
  259. base.enabled = true;
  260. this.mAnim.enabled = false;
  261. bool flag = string.IsNullOrEmpty(clipName);
  262. if (flag)
  263. {
  264. if (!this.mAnim.isPlaying)
  265. {
  266. this.mAnim.Play();
  267. }
  268. }
  269. else if (!this.mAnim.IsPlaying(clipName))
  270. {
  271. this.mAnim.Play(clipName);
  272. }
  273. IEnumerator enumerator = this.mAnim.GetEnumerator();
  274. try
  275. {
  276. while (enumerator.MoveNext())
  277. {
  278. object obj = enumerator.Current;
  279. AnimationState animationState = (AnimationState)obj;
  280. if (string.IsNullOrEmpty(clipName) || animationState.name == clipName)
  281. {
  282. float num = Mathf.Abs(animationState.speed);
  283. animationState.speed = num * (float)playDirection;
  284. if (playDirection == Direction.Reverse && animationState.time == 0f)
  285. {
  286. animationState.time = animationState.length;
  287. }
  288. else if (playDirection == Direction.Forward && animationState.time == animationState.length)
  289. {
  290. animationState.time = 0f;
  291. }
  292. }
  293. }
  294. }
  295. finally
  296. {
  297. IDisposable disposable;
  298. if ((disposable = (enumerator as IDisposable)) != null)
  299. {
  300. disposable.Dispose();
  301. }
  302. }
  303. this.mLastDirection = playDirection;
  304. this.mNotify = true;
  305. this.mAnim.Sample();
  306. }
  307. else if (this.mAnimator != null)
  308. {
  309. if (base.enabled && this.isPlaying && this.mClip == clipName)
  310. {
  311. this.mLastDirection = playDirection;
  312. return;
  313. }
  314. base.enabled = true;
  315. this.mNotify = true;
  316. this.mLastDirection = playDirection;
  317. this.mClip = clipName;
  318. this.mAnimator.Play(this.mClip, 0, (playDirection != Direction.Forward) ? 1f : 0f);
  319. }
  320. }
  321. public static ActiveAnimation Play(Animation anim, string clipName, Direction playDirection, EnableCondition enableBeforePlay, DisableCondition disableCondition)
  322. {
  323. if (!NGUITools.GetActive(anim.gameObject))
  324. {
  325. if (enableBeforePlay != EnableCondition.EnableThenPlay)
  326. {
  327. return null;
  328. }
  329. NGUITools.SetActive(anim.gameObject, true);
  330. UIPanel[] componentsInChildren = anim.gameObject.GetComponentsInChildren<UIPanel>();
  331. int i = 0;
  332. int num = componentsInChildren.Length;
  333. while (i < num)
  334. {
  335. componentsInChildren[i].Refresh();
  336. i++;
  337. }
  338. }
  339. ActiveAnimation activeAnimation = anim.GetComponent<ActiveAnimation>();
  340. if (activeAnimation == null)
  341. {
  342. activeAnimation = anim.gameObject.AddComponent<ActiveAnimation>();
  343. }
  344. activeAnimation.mAnim = anim;
  345. activeAnimation.mDisableDirection = (Direction)disableCondition;
  346. activeAnimation.onFinished.Clear();
  347. activeAnimation.Play(clipName, playDirection);
  348. if (activeAnimation.mAnim != null)
  349. {
  350. activeAnimation.mAnim.Sample();
  351. }
  352. else if (activeAnimation.mAnimator != null)
  353. {
  354. activeAnimation.mAnimator.Update(0f);
  355. }
  356. return activeAnimation;
  357. }
  358. public static ActiveAnimation Play(Animation anim, string clipName, Direction playDirection)
  359. {
  360. return ActiveAnimation.Play(anim, clipName, playDirection, EnableCondition.DoNothing, DisableCondition.DoNotDisable);
  361. }
  362. public static ActiveAnimation Play(Animation anim, Direction playDirection)
  363. {
  364. return ActiveAnimation.Play(anim, null, playDirection, EnableCondition.DoNothing, DisableCondition.DoNotDisable);
  365. }
  366. public static ActiveAnimation Play(Animator anim, string clipName, Direction playDirection, EnableCondition enableBeforePlay, DisableCondition disableCondition)
  367. {
  368. if (enableBeforePlay != EnableCondition.IgnoreDisabledState && !NGUITools.GetActive(anim.gameObject))
  369. {
  370. if (enableBeforePlay != EnableCondition.EnableThenPlay)
  371. {
  372. return null;
  373. }
  374. NGUITools.SetActive(anim.gameObject, true);
  375. UIPanel[] componentsInChildren = anim.gameObject.GetComponentsInChildren<UIPanel>();
  376. int i = 0;
  377. int num = componentsInChildren.Length;
  378. while (i < num)
  379. {
  380. componentsInChildren[i].Refresh();
  381. i++;
  382. }
  383. }
  384. ActiveAnimation activeAnimation = anim.GetComponent<ActiveAnimation>();
  385. if (activeAnimation == null)
  386. {
  387. activeAnimation = anim.gameObject.AddComponent<ActiveAnimation>();
  388. }
  389. activeAnimation.mAnimator = anim;
  390. activeAnimation.mDisableDirection = (Direction)disableCondition;
  391. activeAnimation.onFinished.Clear();
  392. activeAnimation.Play(clipName, playDirection);
  393. if (activeAnimation.mAnim != null)
  394. {
  395. activeAnimation.mAnim.Sample();
  396. }
  397. else if (activeAnimation.mAnimator != null)
  398. {
  399. activeAnimation.mAnimator.Update(0f);
  400. }
  401. return activeAnimation;
  402. }
  403. public static ActiveAnimation current;
  404. public List<EventDelegate> onFinished = new List<EventDelegate>();
  405. [HideInInspector]
  406. public GameObject eventReceiver;
  407. [HideInInspector]
  408. public string callWhenFinished;
  409. private Animation mAnim;
  410. private Direction mLastDirection;
  411. private Direction mDisableDirection;
  412. private bool mNotify;
  413. private Animator mAnimator;
  414. private string mClip = string.Empty;
  415. }