TweenCG.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. Target = target;
  16. IsForward = false;
  17. IsBackward = false;
  18. Duration = duration;
  19. DestActive = destActive;
  20. OriginActive = originActive;
  21. Delta = destination - origin;
  22. Origin = origin;
  23. Destination = destination;
  24. OnForwardStart += () =>
  25. {
  26. Target.SetActive(true);
  27. Target.interactable = false;
  28. };
  29. OnForwardFinish += () =>
  30. {
  31. Target.SetActive(DestActive);
  32. Target.interactable = DestActive;
  33. };
  34. OnBackwardStart += () =>
  35. {
  36. Target.SetActive(true);
  37. Target.interactable = false;
  38. };
  39. OnBackwardFinish += () =>
  40. {
  41. Target.SetActive(OriginActive);
  42. Target.interactable = OriginActive;
  43. };
  44. Func = ManaAnim.FunctionDicF[curve];
  45. }
  46. public override void StartForward()
  47. {
  48. base.StartForward();
  49. if (IsBackward)
  50. {
  51. Timer = Duration - Timer;
  52. }
  53. }
  54. public override void StartBackward()
  55. {
  56. base.StartBackward();
  57. if (IsForward)
  58. {
  59. Timer = Duration - Timer;
  60. }
  61. }
  62. public override bool DoForward()
  63. {
  64. Timer += Time.fixedDeltaTime;
  65. if (Timer > Duration)
  66. {
  67. Target.alpha = Destination;
  68. Timer = 0;
  69. IsForward = false;
  70. OnForwardFinish.Invoke();
  71. ManaAnim.TweenForList.Remove(this);
  72. return true;
  73. }
  74. else
  75. {
  76. Target.alpha = Func(Timer, Duration, Origin, Delta);
  77. return false;
  78. }
  79. }
  80. public override bool DoBackward()
  81. {
  82. Timer += Time.fixedDeltaTime;
  83. if (Timer > Duration)
  84. {
  85. Target.alpha = Origin;
  86. Timer = 0;
  87. IsBackward = false;
  88. OnBackwardFinish.Invoke();
  89. ManaAnim.TweenBacList.Remove(this);
  90. return true;
  91. }
  92. else
  93. {
  94. Target.alpha = Func(Timer, Duration, Destination, -Delta);
  95. return false;
  96. }
  97. }
  98. }