LitMotionAudioExtensions.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #if LITMOTION_SUPPORT_UNITY_AUDIO
  2. using UnityEngine;
  3. using UnityEngine.Audio;
  4. namespace LitMotion.Extensions
  5. {
  6. /// <summary>
  7. /// Provides binding extension methods for AudioSource and AudioMixer.
  8. /// </summary>
  9. public static class LitMotionAudioExtensions
  10. {
  11. /// <summary>
  12. /// Create a motion data and bind it to AudioSource.volume
  13. /// </summary>
  14. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  15. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  16. /// <param name="builder">This builder</param>
  17. /// <param name="transform"></param>
  18. /// <returns>Handle of the created motion data.</returns>
  19. public static MotionHandle BindToVolume<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, AudioSource audioSource)
  20. where TOptions : unmanaged, IMotionOptions
  21. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  22. {
  23. Error.IsNull(audioSource);
  24. return builder.BindWithState(audioSource, static (x, target) =>
  25. {
  26. target.volume = x;
  27. });
  28. }
  29. /// <summary>
  30. /// Create a motion data and bind it to AudioSource.pitch
  31. /// </summary>
  32. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  33. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  34. /// <param name="builder">This builder</param>
  35. /// <param name="transform"></param>
  36. /// <returns>Handle of the created motion data.</returns>
  37. public static MotionHandle BindToPitch<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, AudioSource audioSource)
  38. where TOptions : unmanaged, IMotionOptions
  39. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  40. {
  41. Error.IsNull(audioSource);
  42. return builder.BindWithState(audioSource, static (x, target) =>
  43. {
  44. target.pitch = x;
  45. });
  46. }
  47. /// <summary>
  48. /// Create a motion data and bind it to AudioMixer exposed parameter.
  49. /// </summary>
  50. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  51. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  52. /// <param name="builder">This builder</param>
  53. /// <param name="transform"></param>
  54. /// <returns>Handle of the created motion data.</returns>
  55. public static MotionHandle BindToAudioMixerFloat<TOptions, TAdapter>(this MotionBuilder<float, TOptions, TAdapter> builder, AudioMixer audioMixer, string name)
  56. where TOptions : unmanaged, IMotionOptions
  57. where TAdapter : unmanaged, IMotionAdapter<float, TOptions>
  58. {
  59. Error.IsNull(audioMixer);
  60. return builder.BindWithState(audioMixer, name, static (x, target, n) =>
  61. {
  62. target.SetFloat(n, x);
  63. });
  64. }
  65. }
  66. }
  67. #endif