TweenAudio.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. public class TweenAudio : Tween
  5. {
  6. #region 变量
  7. public override bool InOrigin
  8. {
  9. get
  10. {
  11. if (Math.Abs(Target.volume - 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.volume = Origin;
  27. }
  28. }
  29. }
  30. public override bool InDestination
  31. {
  32. get
  33. {
  34. if (Math.Abs(Target.volume - 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.volume = Destination;
  50. }
  51. }
  52. }
  53. protected float Delta;
  54. protected float Origin;
  55. protected float Destination;
  56. protected AudioSource Target;
  57. protected CurveFunctionF Func;
  58. #endregion
  59. public TweenAudio(AudioSource 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. Destination = destination;
  69. OnForwardStart += () =>
  70. {
  71. };
  72. OnForwardFinish += () =>
  73. {
  74. };
  75. OnBackwardStart += () =>
  76. {
  77. };
  78. OnBackwardFinish += () =>
  79. {
  80. };
  81. }
  82. public override void StartForward()
  83. {
  84. base.StartForward();
  85. if (InBackward)
  86. {
  87. Timer = Duration - Timer;
  88. }
  89. else
  90. {
  91. Target.volume = Origin;
  92. }
  93. }
  94. public override void StartBackward()
  95. {
  96. base.StartBackward();
  97. if (InForward)
  98. {
  99. Timer = Duration - Timer;
  100. }
  101. else
  102. {
  103. Target.volume = Destination;
  104. }
  105. }
  106. public override bool DoForward()
  107. {
  108. Timer += Time.fixedDeltaTime;
  109. if (Timer > Duration)
  110. {
  111. Timer = 0;
  112. InForward = false;
  113. InDestination = true;
  114. OnForwardFinish.Invoke();
  115. ManaAnim.TweenForList.Remove(this);
  116. return true;
  117. }
  118. else
  119. {
  120. Target.volume = Func(Timer, Duration, Origin, Delta);
  121. return false;
  122. }
  123. }
  124. public override bool DoBackward()
  125. {
  126. Timer += Time.fixedDeltaTime;
  127. if (Timer > Duration)
  128. {
  129. Timer = 0;
  130. InBackward = false;
  131. InOrigin = true;
  132. OnBackwardFinish.Invoke();
  133. ManaAnim.TweenBacList.Remove(this);
  134. return true;
  135. }
  136. else
  137. {
  138. Target.volume = Func(Timer, Duration, Destination, -Delta);
  139. return false;
  140. }
  141. }
  142. }