TweenCG.cs 3.6 KB

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