TweenCG.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. };
  74. OnForwardFinish += () =>
  75. {
  76. };
  77. OnBackwardStart += () =>
  78. {
  79. };
  80. OnBackwardFinish += () =>
  81. {
  82. };
  83. }
  84. public override void StartForward()
  85. {
  86. base.StartForward();
  87. Target.SetActive(true);
  88. Target.interactable = false;
  89. if (InBackward)
  90. {
  91. Timer = Duration - Timer;
  92. }
  93. else
  94. {
  95. Target.alpha = Origin;
  96. }
  97. }
  98. public override void StartBackward()
  99. {
  100. base.StartBackward();
  101. Target.SetActive(true);
  102. Target.interactable = false;
  103. if (InForward)
  104. {
  105. Timer = Duration - Timer;
  106. }
  107. else
  108. {
  109. Target.alpha = Destination;
  110. }
  111. }
  112. public override bool DoForward()
  113. {
  114. Timer += Time.fixedDeltaTime;
  115. if (Timer > Duration)
  116. {
  117. Timer = 0;
  118. InForward = false;
  119. InDestination = true;
  120. Target.SetActive(DestActive);
  121. Target.interactable = DestActive;
  122. OnForwardFinish.Invoke();
  123. ManaAnim.TweenForList.Remove(this);
  124. return true;
  125. }
  126. else
  127. {
  128. Target.alpha = Func(Timer, Duration, Origin, Delta);
  129. return false;
  130. }
  131. }
  132. public override bool DoBackward()
  133. {
  134. Timer += Time.fixedDeltaTime;
  135. if (Timer > Duration)
  136. {
  137. Timer = 0;
  138. InBackward = false;
  139. InOrigin = true;
  140. Target.SetActive(OriginActive);
  141. Target.interactable = OriginActive;
  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. }