TweenRect.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using UnityEngine;
  2. using System.Collections;
  3. public class TweenRect : Tween
  4. {
  5. #region
  6. public override bool InOrigin
  7. {
  8. get
  9. {
  10. if (Target.sizeDelta == Origin)
  11. {
  12. _InOrigin = true;
  13. }
  14. else
  15. {
  16. _InOrigin = false;
  17. }
  18. return _InOrigin;
  19. }
  20. set
  21. {
  22. _InOrigin = value;
  23. if (_InOrigin)
  24. {
  25. Target.sizeDelta = Origin;
  26. }
  27. }
  28. }
  29. public override bool InDestination
  30. {
  31. get
  32. {
  33. if (Target.sizeDelta == Destination)
  34. {
  35. _InDestination = true;
  36. }
  37. else
  38. {
  39. _InDestination = false;
  40. }
  41. return _InDestination;
  42. }
  43. set
  44. {
  45. _InDestination = value;
  46. if (_InDestination)
  47. {
  48. Target.sizeDelta = Destination;
  49. }
  50. }
  51. }
  52. protected Vector2 Delta;
  53. protected Vector2 Origin;
  54. protected Vector2 Destination;
  55. protected CanvasGroup CG;
  56. protected RectTransform Target;
  57. protected CurveFunctionV Func;
  58. #endregion
  59. public TweenRect(RectTransform target, Vector2 origin, Vector2 destination, float duration, bool originActive, bool destActive, Curve curve)
  60. {
  61. CG = target.GetComponent<CanvasGroup>();
  62. Func = ManaAnim.CurveFunctionDicV[curve];
  63. Target = target;
  64. InForward = false;
  65. InBackward = false;
  66. Delta = destination - origin;
  67. Origin = origin;
  68. Duration = duration;
  69. DestActive = destActive;
  70. Destination = destination;
  71. OriginActive = originActive;
  72. OnForwardStart += () =>
  73. {
  74. Target.SetActive(true);
  75. if (CG != null)
  76. {
  77. CG.interactable = false;
  78. }
  79. };
  80. OnForwardFinish += () =>
  81. {
  82. Target.SetActive(DestActive);
  83. if (CG != null)
  84. {
  85. CG.interactable = DestActive;
  86. }
  87. };
  88. OnBackwardStart += () =>
  89. {
  90. Target.SetActive(true);
  91. if (CG != null)
  92. {
  93. CG.interactable = false;
  94. }
  95. };
  96. OnBackwardFinish += () =>
  97. {
  98. Target.SetActive(OriginActive);
  99. if (CG != null)
  100. {
  101. CG.interactable = OriginActive;
  102. }
  103. };
  104. }
  105. public override void StartForward()
  106. {
  107. base.StartForward();
  108. if (InBackward)
  109. {
  110. Timer = Duration - Timer;
  111. }
  112. else
  113. {
  114. Target.sizeDelta = Origin;
  115. }
  116. }
  117. public override void StartBackward()
  118. {
  119. base.StartBackward();
  120. if (InForward)
  121. {
  122. Timer = Duration - Timer;
  123. }
  124. else
  125. {
  126. Target.sizeDelta = Destination;
  127. }
  128. }
  129. public override bool DoForward()
  130. {
  131. Timer += Time.fixedDeltaTime;
  132. if (Timer > Duration)
  133. {
  134. Timer = 0;
  135. InForward = false;
  136. InDestination = true;
  137. OnForwardFinish.Invoke();
  138. ManaAnim.TweenForList.Remove(this);
  139. return true;
  140. }
  141. else
  142. {
  143. Target.sizeDelta = Func(Timer, Duration, Origin, Delta);
  144. return false;
  145. }
  146. }
  147. public override bool DoBackward()
  148. {
  149. Timer += Time.fixedDeltaTime;
  150. if (Timer > Duration)
  151. {
  152. Timer = 0;
  153. InBackward = false;
  154. InOrigin = true;
  155. OnBackwardFinish.Invoke();
  156. ManaAnim.TweenBacList.Remove(this);
  157. return true;
  158. }
  159. else
  160. {
  161. Target.sizeDelta = Func(Timer, Duration, Destination, -Delta);
  162. return false;
  163. }
  164. }
  165. }