UIPlayTween.cs 6.3 KB

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