AudioPlayableAsset.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Audio;
  4. #if UNITY_EDITOR
  5. using System.ComponentModel;
  6. #endif
  7. using UnityEngine.Playables;
  8. namespace UnityEngine.Timeline
  9. {
  10. /// <summary>
  11. /// PlayableAsset wrapper for an AudioClip in Timeline.
  12. /// </summary>
  13. [Serializable]
  14. #if UNITY_EDITOR
  15. [DisplayName("Audio Clip")]
  16. #endif
  17. public class AudioPlayableAsset : PlayableAsset, ITimelineClipAsset
  18. {
  19. [SerializeField] AudioClip m_Clip;
  20. #pragma warning disable 649 //Field is never assigned to and will always have its default value
  21. [SerializeField] bool m_Loop;
  22. [SerializeField, HideInInspector] float m_bufferingTime = 0.1f;
  23. [SerializeField] AudioClipProperties m_ClipProperties = new AudioClipProperties();
  24. // the amount of time to give the clip to load prior to it's start time
  25. internal float bufferingTime
  26. {
  27. get { return m_bufferingTime; }
  28. set { m_bufferingTime = value; }
  29. }
  30. #if UNITY_EDITOR
  31. Playable m_LiveClipPlayable = Playable.Null;
  32. #endif
  33. /// <summary>
  34. /// The audio clip to be played
  35. /// </summary>
  36. public AudioClip clip
  37. {
  38. get { return m_Clip; }
  39. set { m_Clip = value; }
  40. }
  41. /// <summary>
  42. /// Whether the audio clip loops.
  43. /// </summary>
  44. /// <remarks>
  45. /// Use this to loop the audio clip when the duration of the timeline clip exceeds that of the audio clip.
  46. /// </remarks>
  47. public bool loop
  48. {
  49. get { return m_Loop; }
  50. set { m_Loop = value; }
  51. }
  52. /// <summary>
  53. /// Returns the duration required to play the audio clip exactly once
  54. /// </summary>
  55. public override double duration
  56. {
  57. get
  58. {
  59. if (m_Clip == null)
  60. return base.duration;
  61. // use this instead of length to avoid rounding precision errors,
  62. return (double)m_Clip.samples / m_Clip.frequency;
  63. }
  64. }
  65. /// <summary>
  66. /// Returns a description of the PlayableOutputs that may be created for this asset.
  67. /// </summary>
  68. public override IEnumerable<PlayableBinding> outputs
  69. {
  70. get { yield return AudioPlayableBinding.Create(name, this); }
  71. }
  72. /// <summary>
  73. /// Creates the root of a Playable subgraph to play the audio clip.
  74. /// </summary>
  75. /// <param name="graph">PlayableGraph that will own the playable</param>
  76. /// <param name="go">The GameObject that triggered the graph build</param>
  77. /// <returns>The root playable of the subgraph</returns>
  78. public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
  79. {
  80. if (m_Clip == null)
  81. return Playable.Null;
  82. var audioClipPlayable = AudioClipPlayable.Create(graph, m_Clip, m_Loop);
  83. audioClipPlayable.GetHandle().SetScriptInstance(m_ClipProperties.Clone());
  84. #if UNITY_EDITOR
  85. m_LiveClipPlayable = audioClipPlayable;
  86. #endif
  87. return audioClipPlayable;
  88. }
  89. /// <summary>
  90. /// Returns the capabilities of TimelineClips that contain an AudioPlayableAsset
  91. /// </summary>
  92. public ClipCaps clipCaps
  93. {
  94. get
  95. {
  96. return ClipCaps.ClipIn |
  97. ClipCaps.SpeedMultiplier |
  98. ClipCaps.Blending |
  99. (m_Loop ? ClipCaps.Looping : ClipCaps.None);
  100. }
  101. }
  102. #if UNITY_EDITOR
  103. internal void LiveLink()
  104. {
  105. if (!m_LiveClipPlayable.IsValid())
  106. return;
  107. var audioMixerProperties = m_LiveClipPlayable.GetHandle().GetObject<AudioClipProperties>();
  108. if (audioMixerProperties == null)
  109. return;
  110. audioMixerProperties.volume = m_ClipProperties.volume;
  111. }
  112. #endif
  113. }
  114. }