TweenAudio.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. }
  70. public override void StartForward()
  71. {
  72. base.StartForward();
  73. if (InBackward)
  74. {
  75. Timer = Duration - Timer;
  76. }
  77. else
  78. {
  79. Target.volume = Origin;
  80. }
  81. }
  82. public override void StartBackward()
  83. {
  84. base.StartBackward();
  85. if (InForward)
  86. {
  87. Timer = Duration - Timer;
  88. }
  89. else
  90. {
  91. Target.volume = Destination;
  92. }
  93. }
  94. public override bool DoForward()
  95. {
  96. Timer += Time.fixedDeltaTime;
  97. if (Timer > Duration)
  98. {
  99. Timer = 0;
  100. InForward = false;
  101. InDestination = true;
  102. if (OnForwardFinish != null)
  103. {
  104. OnForwardFinish.Invoke();
  105. }
  106. ManaAnim.TweenForList.Remove(this);
  107. return true;
  108. }
  109. else
  110. {
  111. Target.volume = Func(Timer, Duration, Origin, Delta);
  112. return false;
  113. }
  114. }
  115. public override bool DoBackward()
  116. {
  117. Timer += Time.fixedDeltaTime;
  118. if (Timer > Duration)
  119. {
  120. Timer = 0;
  121. InBackward = false;
  122. InOrigin = true;
  123. if (OnBackwardFinish != null)
  124. {
  125. OnBackwardFinish.Invoke();
  126. }
  127. ManaAnim.TweenBacList.Remove(this);
  128. return true;
  129. }
  130. else
  131. {
  132. Target.volume = Func(Timer, Duration, Destination, -Delta);
  133. return false;
  134. }
  135. }
  136. }