Tween.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. protected bool _InOrigin;
  18. protected bool _InDestination;
  19. public bool CG;
  20. public bool InForward;
  21. public bool InBackward;
  22. public Component Component;
  23. public UnityAction OnForwardStart;
  24. public UnityAction OnForwardFinish;
  25. public UnityAction OnBackwardStart;
  26. public UnityAction OnBackwardFinish;
  27. public CanvasGroup CanvasGroup;
  28. public float Duration;
  29. protected float Timer;
  30. protected bool DestActive;
  31. protected bool OriginActive;
  32. public abstract bool DoForward();
  33. public abstract bool DoBackward();
  34. protected Tween(bool cg, Component comp)
  35. {
  36. CG = cg;
  37. Component = comp;
  38. if (cg)
  39. {
  40. CanvasGroup = comp.GetComponent<CanvasGroup>();
  41. }
  42. }
  43. public void ConfineForward()
  44. {
  45. if (InOrigin)
  46. {
  47. StartForward();
  48. }
  49. }
  50. public void ConfineBackward()
  51. {
  52. if (InDestination)
  53. {
  54. StartBackward();
  55. }
  56. }
  57. public virtual void StartForward()
  58. {
  59. InForward = true;
  60. Component.SetActive(true);
  61. if (OnForwardStart != null)
  62. {
  63. OnForwardStart.Invoke();
  64. }
  65. if (CG)
  66. {
  67. CanvasGroup.interactable = false;
  68. }
  69. ManaAnim.TweenForList.Add(this);
  70. ManaAnim.TweenBacList.Remove(this);
  71. }
  72. public virtual void StartBackward()
  73. {
  74. InBackward = true;
  75. Component.SetActive(true);
  76. if (OnBackwardStart != null)
  77. {
  78. OnBackwardStart.Invoke();
  79. }
  80. if (CG)
  81. {
  82. CanvasGroup.interactable = false;
  83. }
  84. ManaAnim.TweenForList.Remove(this);
  85. ManaAnim.TweenBacList.Add(this);
  86. }
  87. protected void FinishForward()
  88. {
  89. Component.SetActive(DestActive);
  90. if (CG)
  91. {
  92. CanvasGroup.interactable = DestActive;
  93. }
  94. }
  95. protected void FinishBackward()
  96. {
  97. Component.SetActive(OriginActive);
  98. if (CG)
  99. {
  100. CanvasGroup.interactable = OriginActive;
  101. }
  102. }
  103. public void PushEvent(EventType type, UnityAction action)
  104. {
  105. if (type == EventType.ForwardStart)
  106. {
  107. OnForwardStart = action + OnForwardStart;
  108. }
  109. else if (type == EventType.ForwardFinish)
  110. {
  111. OnForwardFinish = action + OnForwardFinish;
  112. }
  113. else if (type == EventType.BackwardStart)
  114. {
  115. OnBackwardStart = action + OnBackwardStart;
  116. }
  117. else if (type == EventType.BackwardFinish)
  118. {
  119. OnBackwardFinish = action + OnBackwardFinish;
  120. }
  121. }
  122. public void AddEventOnetime(EventType type, UnityAction action)
  123. {
  124. if (type == EventType.ForwardStart)
  125. {
  126. action += () =>
  127. {
  128. OnForwardStart -= action;
  129. };
  130. OnForwardStart += action;
  131. }
  132. else if (type == EventType.ForwardFinish)
  133. {
  134. action += () =>
  135. {
  136. OnForwardFinish -= action;
  137. };
  138. OnForwardFinish += action;
  139. }
  140. else if (type == EventType.BackwardStart)
  141. {
  142. action += () =>
  143. {
  144. OnBackwardStart -= action;
  145. };
  146. OnBackwardStart += action;
  147. }
  148. else if (type == EventType.BackwardFinish)
  149. {
  150. action += () =>
  151. {
  152. OnBackwardFinish -= action;
  153. };
  154. OnBackwardFinish += action;
  155. }
  156. }
  157. public void PushEventOnetime(EventType type, UnityAction action)
  158. {
  159. if (type == EventType.ForwardStart)
  160. {
  161. action += () =>
  162. {
  163. OnForwardStart -= action;
  164. };
  165. OnForwardStart = action + OnForwardStart;
  166. }
  167. else if (type == EventType.ForwardFinish)
  168. {
  169. action += () =>
  170. {
  171. OnForwardFinish -= action;
  172. };
  173. OnForwardFinish = action + OnForwardFinish;
  174. }
  175. else if (type == EventType.BackwardStart)
  176. {
  177. action += () =>
  178. {
  179. OnBackwardStart -= action;
  180. };
  181. OnBackwardStart = action + OnBackwardStart;
  182. }
  183. else if (type == EventType.BackwardFinish)
  184. {
  185. action += () =>
  186. {
  187. OnBackwardFinish -= action;
  188. };
  189. OnBackwardFinish = action + OnBackwardFinish;
  190. }
  191. }
  192. }