DOTweenModuleAudio.cs 8.6 KB

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