TweenAudio.cs 2.6 KB

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