TweenRoot.cs 7.0 KB

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