TweenRoot.cs 6.7 KB

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