TweenAudio.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 (Target.volume.Equal(Origin))
  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. BackwardFinish();
  28. }
  29. }
  30. }
  31. public override bool InDestination
  32. {
  33. get
  34. {
  35. if (Target.volume.Equal(Destination))
  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. ForwardFinish();
  52. }
  53. }
  54. }
  55. public float Delta;
  56. public float Origin;
  57. public float Destination;
  58. public AudioSource Target;
  59. public 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, curve, 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 = ManaAnim.GetTimerFloat(Target.volume, Duration, Origin, Delta, Curve);
  80. }
  81. }
  82. public override void StartBackward()
  83. {
  84. base.StartBackward();
  85. if (InForward)
  86. {
  87. Timer = ManaAnim.GetTimerFloat(Target.volume, Duration, Destination, -Delta, Curve);
  88. }
  89. }
  90. public override bool DoForward()
  91. {
  92. Timer += Time.fixedDeltaTime;
  93. if (Timer > Duration)
  94. {
  95. InForward = false;
  96. InDestination = true;
  97. if (OnForwardFinish != null)
  98. {
  99. OnForwardFinish.Invoke();
  100. }
  101. return true;
  102. }
  103. else
  104. {
  105. Target.volume = Func(Timer, Duration, Origin, Delta);
  106. return false;
  107. }
  108. }
  109. public override bool DoBackward()
  110. {
  111. Timer += Time.fixedDeltaTime;
  112. if (Timer > Duration)
  113. {
  114. InBackward = false;
  115. InOrigin = true;
  116. if (OnBackwardFinish != null)
  117. {
  118. OnBackwardFinish.Invoke();
  119. }
  120. return true;
  121. }
  122. else
  123. {
  124. Target.volume = Func(Timer, Duration, Destination, -Delta);
  125. return false;
  126. }
  127. }
  128. }