Tween.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using System;
  4. using System.Collections;
  5. public abstract class Tween
  6. {
  7. public virtual bool InOrigin
  8. {
  9. get { return _InOrigin; }
  10. set { _InOrigin = value; }
  11. }
  12. public virtual bool InDestination
  13. {
  14. get { return _InDestination; }
  15. set { _InDestination = value; }
  16. }
  17. public bool _InOrigin;
  18. public bool _InDestination;
  19. public bool CG;
  20. public bool InForward;
  21. public bool InBackward;
  22. public Curve Curve;
  23. public Component Component;
  24. public UnityAction OnForwardStart;
  25. public UnityAction OnForwardFinish;
  26. public UnityAction OnBackwardStart;
  27. public UnityAction OnBackwardFinish;
  28. public CanvasGroup CanvasGroup;
  29. public float Duration;
  30. public float Timer;
  31. public bool DestActive;
  32. public bool OriginActive;
  33. public abstract bool DoForward();
  34. public abstract bool DoBackward();
  35. public Tween(bool cg, Curve curve, Component comp)
  36. {
  37. CG = cg;
  38. Curve = curve;
  39. Component = comp;
  40. if (cg)
  41. {
  42. CanvasGroup = comp.GetComponent<CanvasGroup>();
  43. }
  44. }
  45. public void Pause()
  46. {
  47. if (InForward)
  48. {
  49. ManaAnim.TweenForList.Remove(this);
  50. }
  51. else if (InBackward)
  52. {
  53. ManaAnim.TweenBacList.Remove(this);
  54. }
  55. }
  56. public void Resume()
  57. {
  58. if (InForward)
  59. {
  60. ManaAnim.TweenForList.Add(this);
  61. }
  62. else if (InBackward)
  63. {
  64. ManaAnim.TweenBacList.Add(this);
  65. }
  66. }
  67. public void ReForward()
  68. {
  69. Timer = 0;
  70. if (InBackward)
  71. {
  72. InBackward = false;
  73. ManaAnim.TweenBacList.Remove(this);
  74. }
  75. if (!InForward)
  76. {
  77. StartForward();
  78. }
  79. }
  80. public void ReBackward()
  81. {
  82. Timer = 0;
  83. if (InForward)
  84. {
  85. InForward = false;
  86. ManaAnim.TweenForList.Remove(this);
  87. }
  88. if (!InBackward)
  89. {
  90. StartBackward();
  91. }
  92. }
  93. public void ConfineForward()
  94. {
  95. if (!InDestination)
  96. {
  97. StartForward();
  98. }
  99. }
  100. public void ConfineBackward()
  101. {
  102. if (!InOrigin)
  103. {
  104. StartBackward();
  105. }
  106. }
  107. public virtual void StartForward()
  108. {
  109. InForward = true;
  110. Component.SetActive(true);
  111. if (InOrigin)
  112. {
  113. OnForwardStart.SafeInvoke();
  114. }
  115. if (CG)
  116. {
  117. CanvasGroup.interactable = false;
  118. }
  119. if (InBackward)
  120. {
  121. ManaAnim.TweenBacList.Remove(this);
  122. }
  123. ManaAnim.TweenForList.UniqueAdd(this);
  124. }
  125. public virtual void StartBackward()
  126. {
  127. InBackward = true;
  128. Component.SetActive(true);
  129. if (InDestination)
  130. {
  131. OnBackwardStart.SafeInvoke();
  132. }
  133. if (CG)
  134. {
  135. CanvasGroup.interactable = false;
  136. }
  137. if (InForward)
  138. {
  139. ManaAnim.TweenForList.Remove(this);
  140. }
  141. ManaAnim.TweenBacList.UniqueAdd(this);
  142. }
  143. protected void FinishForward()
  144. {
  145. Component.SetActive(DestActive);
  146. if (CG)
  147. {
  148. CanvasGroup.interactable = DestActive;
  149. }
  150. }
  151. protected void FinishBackward()
  152. {
  153. Component.SetActive(OriginActive);
  154. if (CG)
  155. {
  156. CanvasGroup.interactable = OriginActive;
  157. }
  158. }
  159. public void PushEvent(EventType type, UnityAction action)
  160. {
  161. if (type == EventType.ForwardStart)
  162. {
  163. OnForwardStart = action + OnForwardStart;
  164. }
  165. else if (type == EventType.ForwardFinish)
  166. {
  167. OnForwardFinish = action + OnForwardFinish;
  168. }
  169. else if (type == EventType.BackwardStart)
  170. {
  171. OnBackwardStart = action + OnBackwardStart;
  172. }
  173. else if (type == EventType.BackwardFinish)
  174. {
  175. OnBackwardFinish = action + OnBackwardFinish;
  176. }
  177. }
  178. public void AddEventOnetime(EventType type, UnityAction action)
  179. {
  180. if (type == EventType.ForwardStart)
  181. {
  182. action += () =>
  183. {
  184. OnForwardStart -= action;
  185. };
  186. OnForwardStart += action;
  187. }
  188. else if (type == EventType.ForwardFinish)
  189. {
  190. action += () =>
  191. {
  192. OnForwardFinish -= action;
  193. };
  194. OnForwardFinish += action;
  195. }
  196. else if (type == EventType.BackwardStart)
  197. {
  198. action += () =>
  199. {
  200. OnBackwardStart -= action;
  201. };
  202. OnBackwardStart += action;
  203. }
  204. else if (type == EventType.BackwardFinish)
  205. {
  206. action += () =>
  207. {
  208. OnBackwardFinish -= action;
  209. };
  210. OnBackwardFinish += action;
  211. }
  212. }
  213. public void PushEventOnetime(EventType type, UnityAction action)
  214. {
  215. if (type == EventType.ForwardStart)
  216. {
  217. action += () =>
  218. {
  219. OnForwardStart -= action;
  220. };
  221. OnForwardStart = action + OnForwardStart;
  222. }
  223. else if (type == EventType.ForwardFinish)
  224. {
  225. action += () =>
  226. {
  227. OnForwardFinish -= action;
  228. };
  229. OnForwardFinish = action + OnForwardFinish;
  230. }
  231. else if (type == EventType.BackwardStart)
  232. {
  233. action += () =>
  234. {
  235. OnBackwardStart -= action;
  236. };
  237. OnBackwardStart = action + OnBackwardStart;
  238. }
  239. else if (type == EventType.BackwardFinish)
  240. {
  241. action += () =>
  242. {
  243. OnBackwardFinish -= action;
  244. };
  245. OnBackwardFinish = action + OnBackwardFinish;
  246. }
  247. }
  248. }