UIPlayAnimation.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Collections.Generic;
  3. using AnimationOrTween;
  4. using UnityEngine;
  5. [ExecuteInEditMode]
  6. [AddComponentMenu("NGUI/Interaction/Play Animation")]
  7. public class UIPlayAnimation : MonoBehaviour
  8. {
  9. private bool dualState
  10. {
  11. get
  12. {
  13. return this.trigger == Trigger.OnPress || this.trigger == Trigger.OnHover;
  14. }
  15. }
  16. private void Awake()
  17. {
  18. UIButton component = base.GetComponent<UIButton>();
  19. if (component != null)
  20. {
  21. this.dragHighlight = component.dragHighlight;
  22. }
  23. if (this.eventReceiver != null && EventDelegate.IsValid(this.onFinished))
  24. {
  25. this.eventReceiver = null;
  26. this.callWhenFinished = null;
  27. }
  28. }
  29. private void Start()
  30. {
  31. this.mStarted = true;
  32. if (this.target == null && this.animator == null)
  33. {
  34. this.animator = base.GetComponentInChildren<Animator>();
  35. }
  36. if (this.animator != null)
  37. {
  38. if (this.animator.enabled)
  39. {
  40. this.animator.enabled = false;
  41. }
  42. return;
  43. }
  44. if (this.target == null)
  45. {
  46. this.target = base.GetComponentInChildren<Animation>();
  47. }
  48. if (this.target != null && this.target.enabled)
  49. {
  50. this.target.enabled = false;
  51. }
  52. }
  53. private void OnEnable()
  54. {
  55. if (this.mStarted)
  56. {
  57. this.OnHover(UICamera.IsHighlighted(base.gameObject));
  58. }
  59. if (UICamera.currentTouch != null)
  60. {
  61. if (this.trigger == Trigger.OnPress || this.trigger == Trigger.OnPressTrue)
  62. {
  63. this.mActivated = (UICamera.currentTouch.pressed == base.gameObject);
  64. }
  65. if (this.trigger == Trigger.OnHover || this.trigger == Trigger.OnHoverTrue)
  66. {
  67. this.mActivated = (UICamera.currentTouch.current == base.gameObject);
  68. }
  69. }
  70. UIToggle component = base.GetComponent<UIToggle>();
  71. if (component != null)
  72. {
  73. EventDelegate.Add(component.onChange, new EventDelegate.Callback(this.OnToggle));
  74. }
  75. }
  76. private void OnDisable()
  77. {
  78. UIToggle component = base.GetComponent<UIToggle>();
  79. if (component != null)
  80. {
  81. EventDelegate.Remove(component.onChange, new EventDelegate.Callback(this.OnToggle));
  82. }
  83. }
  84. private void OnHover(bool isOver)
  85. {
  86. if (!base.enabled)
  87. {
  88. return;
  89. }
  90. if (this.trigger == Trigger.OnHover || (this.trigger == Trigger.OnHoverTrue && isOver) || (this.trigger == Trigger.OnHoverFalse && !isOver))
  91. {
  92. this.Play(isOver, this.dualState);
  93. }
  94. }
  95. private void OnPress(bool isPressed)
  96. {
  97. if (!base.enabled)
  98. {
  99. return;
  100. }
  101. if (UICamera.currentTouchID < -1)
  102. {
  103. return;
  104. }
  105. if (this.trigger == Trigger.OnPress || (this.trigger == Trigger.OnPressTrue && isPressed) || (this.trigger == Trigger.OnPressFalse && !isPressed))
  106. {
  107. this.Play(isPressed, this.dualState);
  108. }
  109. }
  110. private void OnClick()
  111. {
  112. if (UICamera.currentTouchID < -1)
  113. {
  114. return;
  115. }
  116. if (base.enabled && this.trigger == Trigger.OnClick)
  117. {
  118. this.Play(true, false);
  119. }
  120. }
  121. private void OnDoubleClick()
  122. {
  123. if (UICamera.currentTouchID < -1)
  124. {
  125. return;
  126. }
  127. if (base.enabled && this.trigger == Trigger.OnDoubleClick)
  128. {
  129. this.Play(true, false);
  130. }
  131. }
  132. private void OnSelect(bool isSelected)
  133. {
  134. if (!base.enabled)
  135. {
  136. return;
  137. }
  138. if (this.trigger == Trigger.OnSelect || (this.trigger == Trigger.OnSelectTrue && isSelected) || (this.trigger == Trigger.OnSelectFalse && !isSelected))
  139. {
  140. this.Play(isSelected, this.dualState);
  141. }
  142. }
  143. private void OnToggle()
  144. {
  145. if (!base.enabled || UIToggle.current == null)
  146. {
  147. return;
  148. }
  149. if (this.trigger == Trigger.OnActivate || (this.trigger == Trigger.OnActivateTrue && UIToggle.current.value) || (this.trigger == Trigger.OnActivateFalse && !UIToggle.current.value))
  150. {
  151. this.Play(UIToggle.current.value, this.dualState);
  152. }
  153. }
  154. private void OnDragOver()
  155. {
  156. if (base.enabled && this.dualState)
  157. {
  158. if (UICamera.currentTouch.dragged == base.gameObject)
  159. {
  160. this.Play(true, true);
  161. }
  162. else if (this.dragHighlight && this.trigger == Trigger.OnPress)
  163. {
  164. this.Play(true, true);
  165. }
  166. }
  167. }
  168. private void OnDragOut()
  169. {
  170. if (base.enabled && this.dualState && UICamera.hoveredObject != base.gameObject)
  171. {
  172. this.Play(false, true);
  173. }
  174. }
  175. private void OnDrop(GameObject go)
  176. {
  177. if (base.enabled && this.trigger == Trigger.OnPress && UICamera.currentTouch.dragged != base.gameObject)
  178. {
  179. this.Play(false, true);
  180. }
  181. }
  182. public void Play(bool forward)
  183. {
  184. this.Play(forward, true);
  185. }
  186. public void Play(bool forward, bool onlyIfDifferent)
  187. {
  188. if (this.target || this.animator)
  189. {
  190. if (onlyIfDifferent)
  191. {
  192. if (this.mActivated == forward)
  193. {
  194. return;
  195. }
  196. this.mActivated = forward;
  197. }
  198. if (this.clearSelection && UICamera.selectedObject == base.gameObject)
  199. {
  200. UICamera.selectedObject = null;
  201. }
  202. int num = (int)(-(int)this.playDirection);
  203. Direction direction = (Direction)((!forward) ? num : ((int)this.playDirection));
  204. ActiveAnimation activeAnimation = (!this.target) ? ActiveAnimation.Play(this.animator, this.clipName, direction, this.ifDisabledOnPlay, this.disableWhenFinished) : ActiveAnimation.Play(this.target, this.clipName, direction, this.ifDisabledOnPlay, this.disableWhenFinished);
  205. if (activeAnimation != null)
  206. {
  207. if (this.resetOnPlay)
  208. {
  209. activeAnimation.Reset();
  210. }
  211. for (int i = 0; i < this.onFinished.Count; i++)
  212. {
  213. EventDelegate.Add(activeAnimation.onFinished, new EventDelegate.Callback(this.OnFinished), true);
  214. }
  215. }
  216. }
  217. }
  218. private void OnFinished()
  219. {
  220. if (UIPlayAnimation.current == null)
  221. {
  222. UIPlayAnimation.current = this;
  223. EventDelegate.Execute(this.onFinished);
  224. if (this.eventReceiver != null && !string.IsNullOrEmpty(this.callWhenFinished))
  225. {
  226. this.eventReceiver.SendMessage(this.callWhenFinished, SendMessageOptions.DontRequireReceiver);
  227. }
  228. this.eventReceiver = null;
  229. UIPlayAnimation.current = null;
  230. }
  231. }
  232. public static UIPlayAnimation current;
  233. public Animation target;
  234. public Animator animator;
  235. public string clipName;
  236. public Trigger trigger;
  237. public Direction playDirection = Direction.Forward;
  238. public bool resetOnPlay;
  239. public bool clearSelection;
  240. public EnableCondition ifDisabledOnPlay;
  241. public DisableCondition disableWhenFinished;
  242. public List<EventDelegate> onFinished = new List<EventDelegate>();
  243. [HideInInspector]
  244. [SerializeField]
  245. private GameObject eventReceiver;
  246. [HideInInspector]
  247. [SerializeField]
  248. private string callWhenFinished;
  249. private bool mStarted;
  250. private bool mActivated;
  251. private bool dragHighlight;
  252. }