TweenAudio.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. FinishBackward();
  28. }
  29. }
  30. }
  31. public override bool InDestination
  32. {
  33. get
  34. {
  35. if (Math.Abs(Target.volume - Destination) < 0.0005f)
  36. {
  37. _InDestination = true;
  38. }
  39. else
  40. {
  41. _InDestination = false;
  42. }
  43. return _InDestination;
  44. }
  45. set
  46. {
  47. _InDestination = value;
  48. if (_InDestination)
  49. {
  50. Target.volume = Destination;
  51. FinishForward();
  52. }
  53. }
  54. }
  55. protected float Delta;
  56. protected float Origin;
  57. protected float Destination;
  58. protected AudioSource Target;
  59. protected CurveFunctionF Func;
  60. #endregion
  61. public TweenAudio(AudioSource target, float origin, float destination, float duration, bool originActive, bool destActive, Curve curve, bool cg = false) : base(cg, target)
  62. {
  63. Func = ManaAnim.CurveFuncDicF[curve];
  64. Target = target;
  65. InForward = false;
  66. InBackward = false;
  67. Delta = destination - origin;
  68. Origin = origin;
  69. Duration = duration;
  70. Destination = destination;
  71. DestActive = destActive;
  72. OriginActive = originActive;
  73. }
  74. public override void StartForward()
  75. {
  76. base.StartForward();
  77. if (InBackward)
  78. {
  79. Timer = Duration - Timer;
  80. }
  81. else
  82. {
  83. Target.volume = Origin;
  84. }
  85. }
  86. public override void StartBackward()
  87. {
  88. base.StartBackward();
  89. if (InForward)
  90. {
  91. Timer = Duration - Timer;
  92. }
  93. else
  94. {
  95. Target.volume = Destination;
  96. }
  97. }
  98. public override bool DoForward()
  99. {
  100. Timer += Time.fixedDeltaTime;
  101. if (Timer > Duration)
  102. {
  103. FinishForward();
  104. Timer = 0;
  105. InForward = false;
  106. InDestination = true;
  107. if (OnForwardFinish != null)
  108. {
  109. OnForwardFinish.Invoke();
  110. }
  111. return true;
  112. }
  113. else
  114. {
  115. Target.volume = Func(Timer, Duration, Origin, Delta);
  116. return false;
  117. }
  118. }
  119. public override bool DoBackward()
  120. {
  121. Timer += Time.fixedDeltaTime;
  122. if (Timer > Duration)
  123. {
  124. FinishBackward();
  125. Timer = 0;
  126. InBackward = false;
  127. InOrigin = true;
  128. if (OnBackwardFinish != null)
  129. {
  130. OnBackwardFinish.Invoke();
  131. }
  132. return true;
  133. }
  134. else
  135. {
  136. Target.volume = Func(Timer, Duration, Destination, -Delta);
  137. return false;
  138. }
  139. }
  140. }