Tween.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 Loop;
  21. public bool PingPong;
  22. public bool InForward;
  23. public bool InBackward;
  24. public Curve Curve;
  25. public Component Component;
  26. public UnityAction OnForwardStart;
  27. public UnityAction OnForwardFinish;
  28. public UnityAction OnBackwardStart;
  29. public UnityAction OnBackwardFinish;
  30. public CanvasGroup CanvasGroup;
  31. public float Duration;
  32. public float Timer;
  33. public bool DestActive;
  34. public bool OriginActive;
  35. public abstract bool DoForward();
  36. public abstract bool DoBackward();
  37. public Tween(bool cg, Curve curve, Component comp)
  38. {
  39. CG = cg;
  40. Curve = curve;
  41. Component = comp;
  42. if (cg)
  43. {
  44. CanvasGroup = comp.GetComponent<CanvasGroup>();
  45. }
  46. }
  47. public void Pause()
  48. {
  49. if (InForward)
  50. {
  51. ManaAnim.TweenForList.Remove(this);
  52. }
  53. else if (InBackward)
  54. {
  55. ManaAnim.TweenBacList.Remove(this);
  56. }
  57. }
  58. public void Resume()
  59. {
  60. if (InForward)
  61. {
  62. ManaAnim.TweenForList.Add(this);
  63. }
  64. else if (InBackward)
  65. {
  66. ManaAnim.TweenBacList.Add(this);
  67. }
  68. }
  69. public void ReForward()
  70. {
  71. Timer = 0;
  72. InOrigin = true;
  73. if (InBackward)
  74. {
  75. InBackward = false;
  76. ManaAnim.TweenBacList.Remove(this);
  77. }
  78. if (!InForward)
  79. {
  80. StartForward();
  81. }
  82. }
  83. public void ReBackward()
  84. {
  85. Timer = 0;
  86. InDestination = true;
  87. if (InForward)
  88. {
  89. InForward = false;
  90. ManaAnim.TweenForList.Remove(this);
  91. }
  92. if (!InBackward)
  93. {
  94. StartBackward();
  95. }
  96. }
  97. public virtual void StartForward()
  98. {
  99. if (InForward || InDestination)
  100. {
  101. return;
  102. }
  103. Timer = 0;
  104. InForward = true;
  105. Component.SetActive(true);
  106. if (InOrigin)
  107. {
  108. OnForwardStart.SafeInvoke();
  109. }
  110. if (CG)
  111. {
  112. CanvasGroup.interactable = false;
  113. }
  114. if (InBackward)
  115. {
  116. ManaAnim.TweenBacList.Remove(this);
  117. }
  118. ManaAnim.TweenForList.Add(this);
  119. }
  120. public virtual void StartBackward()
  121. {
  122. if (InBackward || InOrigin)
  123. {
  124. return;
  125. }
  126. Timer = 0;
  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.Add(this);
  142. }
  143. protected void ForwardFinish()
  144. {
  145. Component.SetActive(DestActive);
  146. if (CG)
  147. {
  148. CanvasGroup.interactable = DestActive;
  149. }
  150. if (Loop)
  151. {
  152. ManaAnim.TweenForList.Add(this);
  153. ReForward();
  154. }
  155. else if (PingPong)
  156. {
  157. StartBackward();
  158. }
  159. }
  160. protected void BackwardFinish()
  161. {
  162. Component.SetActive(OriginActive);
  163. if (CG)
  164. {
  165. CanvasGroup.interactable = OriginActive;
  166. }
  167. if (Loop)
  168. {
  169. ManaAnim.TweenBacList.Add(this);
  170. ReBackward();
  171. }
  172. else if (PingPong)
  173. {
  174. StartForward();
  175. }
  176. }
  177. public void PushEvent(EventType type, UnityAction action)
  178. {
  179. if (type == EventType.ForwardStart)
  180. {
  181. OnForwardStart = action + OnForwardStart;
  182. }
  183. else if (type == EventType.ForwardFinish)
  184. {
  185. OnForwardFinish = action + OnForwardFinish;
  186. }
  187. else if (type == EventType.BackwardStart)
  188. {
  189. OnBackwardStart = action + OnBackwardStart;
  190. }
  191. else if (type == EventType.BackwardFinish)
  192. {
  193. OnBackwardFinish = action + OnBackwardFinish;
  194. }
  195. }
  196. public void AddEventOnetime(EventType type, UnityAction action)
  197. {
  198. if (type == EventType.ForwardStart)
  199. {
  200. action += () =>
  201. {
  202. OnForwardStart -= action;
  203. };
  204. OnForwardStart += action;
  205. }
  206. else if (type == EventType.ForwardFinish)
  207. {
  208. action += () =>
  209. {
  210. OnForwardFinish -= action;
  211. };
  212. OnForwardFinish += action;
  213. }
  214. else if (type == EventType.BackwardStart)
  215. {
  216. action += () =>
  217. {
  218. OnBackwardStart -= action;
  219. };
  220. OnBackwardStart += action;
  221. }
  222. else if (type == EventType.BackwardFinish)
  223. {
  224. action += () =>
  225. {
  226. OnBackwardFinish -= action;
  227. };
  228. OnBackwardFinish += action;
  229. }
  230. }
  231. public void PushEventOnetime(EventType type, UnityAction action)
  232. {
  233. if (type == EventType.ForwardStart)
  234. {
  235. action += () =>
  236. {
  237. OnForwardStart -= action;
  238. };
  239. OnForwardStart = action + OnForwardStart;
  240. }
  241. else if (type == EventType.ForwardFinish)
  242. {
  243. action += () =>
  244. {
  245. OnForwardFinish -= action;
  246. };
  247. OnForwardFinish = action + OnForwardFinish;
  248. }
  249. else if (type == EventType.BackwardStart)
  250. {
  251. action += () =>
  252. {
  253. OnBackwardStart -= action;
  254. };
  255. OnBackwardStart = action + OnBackwardStart;
  256. }
  257. else if (type == EventType.BackwardFinish)
  258. {
  259. action += () =>
  260. {
  261. OnBackwardFinish -= action;
  262. };
  263. OnBackwardFinish = action + OnBackwardFinish;
  264. }
  265. }
  266. }