Tween.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 ReForward()
  46. {
  47. Timer = 0;
  48. if (InBackward)
  49. {
  50. InBackward = false;
  51. ManaAnim.TweenBacList.Remove(this);
  52. }
  53. if (!InForward)
  54. {
  55. StartForward();
  56. }
  57. }
  58. public void ReBackward()
  59. {
  60. Timer = 0;
  61. if (InForward)
  62. {
  63. InForward = false;
  64. ManaAnim.TweenForList.Remove(this);
  65. }
  66. if (!InBackward)
  67. {
  68. StartBackward();
  69. }
  70. }
  71. public void ConfineForward()
  72. {
  73. if (!InDestination)
  74. {
  75. StartForward();
  76. }
  77. }
  78. public void ConfineBackward()
  79. {
  80. if (!InOrigin)
  81. {
  82. StartBackward();
  83. }
  84. }
  85. public virtual void StartForward()
  86. {
  87. InForward = true;
  88. Component.SetActive(true);
  89. if (InOrigin)
  90. {
  91. OnForwardStart.SafeInvoke();
  92. }
  93. if (CG)
  94. {
  95. CanvasGroup.interactable = false;
  96. }
  97. if (InBackward)
  98. {
  99. ManaAnim.TweenBacList.Remove(this);
  100. }
  101. ManaAnim.TweenForList.UniqueAdd(this);
  102. }
  103. public virtual void StartBackward()
  104. {
  105. InBackward = true;
  106. Component.SetActive(true);
  107. if (InDestination)
  108. {
  109. OnBackwardStart.SafeInvoke();
  110. }
  111. if (CG)
  112. {
  113. CanvasGroup.interactable = false;
  114. }
  115. if (InForward)
  116. {
  117. ManaAnim.TweenForList.Remove(this);
  118. }
  119. ManaAnim.TweenBacList.UniqueAdd(this);
  120. }
  121. protected void FinishForward()
  122. {
  123. Component.SetActive(DestActive);
  124. if (CG)
  125. {
  126. CanvasGroup.interactable = DestActive;
  127. }
  128. }
  129. protected void FinishBackward()
  130. {
  131. Component.SetActive(OriginActive);
  132. if (CG)
  133. {
  134. CanvasGroup.interactable = OriginActive;
  135. }
  136. }
  137. public void PushEvent(EventType type, UnityAction action)
  138. {
  139. if (type == EventType.ForwardStart)
  140. {
  141. OnForwardStart = action + OnForwardStart;
  142. }
  143. else if (type == EventType.ForwardFinish)
  144. {
  145. OnForwardFinish = action + OnForwardFinish;
  146. }
  147. else if (type == EventType.BackwardStart)
  148. {
  149. OnBackwardStart = action + OnBackwardStart;
  150. }
  151. else if (type == EventType.BackwardFinish)
  152. {
  153. OnBackwardFinish = action + OnBackwardFinish;
  154. }
  155. }
  156. public void AddEventOnetime(EventType type, UnityAction action)
  157. {
  158. if (type == EventType.ForwardStart)
  159. {
  160. action += () =>
  161. {
  162. OnForwardStart -= action;
  163. };
  164. OnForwardStart += action;
  165. }
  166. else if (type == EventType.ForwardFinish)
  167. {
  168. action += () =>
  169. {
  170. OnForwardFinish -= action;
  171. };
  172. OnForwardFinish += action;
  173. }
  174. else if (type == EventType.BackwardStart)
  175. {
  176. action += () =>
  177. {
  178. OnBackwardStart -= action;
  179. };
  180. OnBackwardStart += action;
  181. }
  182. else if (type == EventType.BackwardFinish)
  183. {
  184. action += () =>
  185. {
  186. OnBackwardFinish -= action;
  187. };
  188. OnBackwardFinish += action;
  189. }
  190. }
  191. public void PushEventOnetime(EventType type, UnityAction action)
  192. {
  193. if (type == EventType.ForwardStart)
  194. {
  195. action += () =>
  196. {
  197. OnForwardStart -= action;
  198. };
  199. OnForwardStart = action + OnForwardStart;
  200. }
  201. else if (type == EventType.ForwardFinish)
  202. {
  203. action += () =>
  204. {
  205. OnForwardFinish -= action;
  206. };
  207. OnForwardFinish = action + OnForwardFinish;
  208. }
  209. else if (type == EventType.BackwardStart)
  210. {
  211. action += () =>
  212. {
  213. OnBackwardStart -= action;
  214. };
  215. OnBackwardStart = action + OnBackwardStart;
  216. }
  217. else if (type == EventType.BackwardFinish)
  218. {
  219. action += () =>
  220. {
  221. OnBackwardFinish -= action;
  222. };
  223. OnBackwardFinish = action + OnBackwardFinish;
  224. }
  225. }
  226. }