TweenCG.cs 3.0 KB

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