DOTweenModuleAudio.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2018/07/13
  3. #if true // MODULE_MARKER
  4. using System;
  5. using DG.Tweening.Core;
  6. using DG.Tweening.Plugins.Options;
  7. using UnityEngine;
  8. using UnityEngine.Audio; // Required for AudioMixer
  9. #pragma warning disable 1591
  10. namespace DG.Tweening
  11. {
  12. public static class DOTweenModuleAudio
  13. {
  14. #region Shortcuts
  15. #region Audio
  16. /// <summary>Tweens an AudioSource's volume to the given value.
  17. /// Also stores the AudioSource as the tween's target so it can be used for filtered operations</summary>
  18. /// <param name="endValue">The end value to reach (0 to 1)</param><param name="duration">The duration of the tween</param>
  19. public static TweenerCore<float, float, FloatOptions> DOFade(this AudioSource target, float endValue, float duration)
  20. {
  21. if (endValue < 0) endValue = 0;
  22. else if (endValue > 1) endValue = 1;
  23. TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.volume, x => target.volume = x, endValue, duration);
  24. t.SetTarget(target);
  25. return t;
  26. }
  27. /// <summary>Tweens an AudioSource's pitch to the given value.
  28. /// Also stores the AudioSource as the tween's target so it can be used for filtered operations</summary>
  29. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  30. public static TweenerCore<float, float, FloatOptions> DOPitch(this AudioSource target, float endValue, float duration)
  31. {
  32. TweenerCore<float, float, FloatOptions> t = DOTween.To(() => target.pitch, x => target.pitch = x, endValue, duration);
  33. t.SetTarget(target);
  34. return t;
  35. }
  36. #endregion
  37. #region AudioMixer
  38. /// <summary>Tweens an AudioMixer's exposed float to the given value.
  39. /// Also stores the AudioMixer as the tween's target so it can be used for filtered operations.
  40. /// Note that you need to manually expose a float in an AudioMixerGroup in order to be able to tween it from an AudioMixer.</summary>
  41. /// <param name="floatName">Name given to the exposed float to set</param>
  42. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  43. public static TweenerCore<float, float, FloatOptions> DOSetFloat(this AudioMixer target, string floatName, float endValue, float duration)
  44. {
  45. TweenerCore<float, float, FloatOptions> t = DOTween.To(()=> {
  46. float currVal;
  47. target.GetFloat(floatName, out currVal);
  48. return currVal;
  49. }, x=> target.SetFloat(floatName, x), endValue, duration);
  50. t.SetTarget(target);
  51. return t;
  52. }
  53. #region Operation Shortcuts
  54. /// <summary>
  55. /// Completes all tweens that have this target as a reference
  56. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  57. /// and returns the total number of tweens completed
  58. /// (meaning the tweens that don't have infinite loops and were not already complete)
  59. /// </summary>
  60. /// <param name="withCallbacks">For Sequences only: if TRUE also internal Sequence callbacks will be fired,
  61. /// otherwise they will be ignored</param>
  62. public static int DOComplete(this AudioMixer target, bool withCallbacks = false)
  63. {
  64. return DOTween.Complete(target, withCallbacks);
  65. }
  66. /// <summary>
  67. /// Kills all tweens that have this target as a reference
  68. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  69. /// and returns the total number of tweens killed.
  70. /// </summary>
  71. /// <param name="complete">If TRUE completes the tween before killing it</param>
  72. public static int DOKill(this AudioMixer target, bool complete = false)
  73. {
  74. return DOTween.Kill(target, complete);
  75. }
  76. /// <summary>
  77. /// Flips the direction (backwards if it was going forward or viceversa) of all tweens that have this target as a reference
  78. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  79. /// and returns the total number of tweens flipped.
  80. /// </summary>
  81. public static int DOFlip(this AudioMixer target)
  82. {
  83. return DOTween.Flip(target);
  84. }
  85. /// <summary>
  86. /// Sends to the given position all tweens that have this target as a reference
  87. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  88. /// and returns the total number of tweens involved.
  89. /// </summary>
  90. /// <param name="to">Time position to reach
  91. /// (if higher than the whole tween duration the tween will simply reach its end)</param>
  92. /// <param name="andPlay">If TRUE will play the tween after reaching the given position, otherwise it will pause it</param>
  93. public static int DOGoto(this AudioMixer target, float to, bool andPlay = false)
  94. {
  95. return DOTween.Goto(target, to, andPlay);
  96. }
  97. /// <summary>
  98. /// Pauses all tweens that have this target as a reference
  99. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  100. /// and returns the total number of tweens paused.
  101. /// </summary>
  102. public static int DOPause(this AudioMixer target)
  103. {
  104. return DOTween.Pause(target);
  105. }
  106. /// <summary>
  107. /// Plays all tweens that have this target as a reference
  108. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  109. /// and returns the total number of tweens played.
  110. /// </summary>
  111. public static int DOPlay(this AudioMixer target)
  112. {
  113. return DOTween.Play(target);
  114. }
  115. /// <summary>
  116. /// Plays backwards all tweens that have this target as a reference
  117. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  118. /// and returns the total number of tweens played.
  119. /// </summary>
  120. public static int DOPlayBackwards(this AudioMixer target)
  121. {
  122. return DOTween.PlayBackwards(target);
  123. }
  124. /// <summary>
  125. /// Plays forward all tweens that have this target as a reference
  126. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  127. /// and returns the total number of tweens played.
  128. /// </summary>
  129. public static int DOPlayForward(this AudioMixer target)
  130. {
  131. return DOTween.PlayForward(target);
  132. }
  133. /// <summary>
  134. /// Restarts all tweens that have this target as a reference
  135. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  136. /// and returns the total number of tweens restarted.
  137. /// </summary>
  138. public static int DORestart(this AudioMixer target)
  139. {
  140. return DOTween.Restart(target);
  141. }
  142. /// <summary>
  143. /// Rewinds all tweens that have this target as a reference
  144. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  145. /// and returns the total number of tweens rewinded.
  146. /// </summary>
  147. public static int DORewind(this AudioMixer target)
  148. {
  149. return DOTween.Rewind(target);
  150. }
  151. /// <summary>
  152. /// Smoothly rewinds all tweens that have this target as a reference
  153. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  154. /// and returns the total number of tweens rewinded.
  155. /// </summary>
  156. public static int DOSmoothRewind(this AudioMixer target)
  157. {
  158. return DOTween.SmoothRewind(target);
  159. }
  160. /// <summary>
  161. /// Toggles the paused state (plays if it was paused, pauses if it was playing) of all tweens that have this target as a reference
  162. /// (meaning tweens that were started from this target, or that had this target added as an Id)
  163. /// and returns the total number of tweens involved.
  164. /// </summary>
  165. public static int DOTogglePause(this AudioMixer target)
  166. {
  167. return DOTween.TogglePause(target);
  168. }
  169. #endregion
  170. #endregion
  171. #endregion
  172. }
  173. }
  174. #endif