AudioTrack.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Audio;
  4. using UnityEngine.Playables;
  5. namespace UnityEngine.Timeline
  6. {
  7. /// <summary>
  8. /// A Timeline track that can play AudioClips.
  9. /// </summary>
  10. [Serializable]
  11. [TrackClipType(typeof(AudioPlayableAsset), false)]
  12. [TrackBindingType(typeof(AudioSource))]
  13. public class AudioTrack : TrackAsset
  14. {
  15. [SerializeField]
  16. AudioMixerProperties m_TrackProperties = new AudioMixerProperties();
  17. #if UNITY_EDITOR
  18. Playable m_LiveMixerPlayable = Playable.Null;
  19. #endif
  20. /// <summary>
  21. /// Create an TimelineClip for playing an AudioClip on this track.
  22. /// </summary>
  23. /// <param name="clip">The audio clip to play</param>
  24. /// <returns>A TimelineClip with an AudioPlayableAsset asset.</returns>
  25. public TimelineClip CreateClip(AudioClip clip)
  26. {
  27. if (clip == null)
  28. return null;
  29. var newClip = CreateDefaultClip();
  30. var audioAsset = newClip.asset as AudioPlayableAsset;
  31. if (audioAsset != null)
  32. audioAsset.clip = clip;
  33. newClip.duration = clip.length;
  34. newClip.displayName = clip.name;
  35. return newClip;
  36. }
  37. internal override Playable CompileClips(PlayableGraph graph, GameObject go, IList<TimelineClip> timelineClips, IntervalTree<RuntimeElement> tree)
  38. {
  39. var clipBlender = AudioMixerPlayable.Create(graph, timelineClips.Count);
  40. #if UNITY_EDITOR
  41. clipBlender.GetHandle().SetScriptInstance(m_TrackProperties.Clone());
  42. m_LiveMixerPlayable = clipBlender;
  43. #else
  44. if (hasCurves)
  45. clipBlender.GetHandle().SetScriptInstance(m_TrackProperties.Clone());
  46. #endif
  47. for (int i = 0; i < timelineClips.Count; i++)
  48. {
  49. var c = timelineClips[i];
  50. var asset = c.asset as PlayableAsset;
  51. if (asset == null)
  52. continue;
  53. var buffer = 0.1f;
  54. var audioAsset = c.asset as AudioPlayableAsset;
  55. if (audioAsset != null)
  56. buffer = audioAsset.bufferingTime;
  57. var source = asset.CreatePlayable(graph, go);
  58. if (!source.IsValid())
  59. continue;
  60. if (source.IsPlayableOfType<AudioClipPlayable>())
  61. {
  62. // Enforce initial values on all clips
  63. var audioClipPlayable = (AudioClipPlayable)source;
  64. var audioClipProperties = audioClipPlayable.GetHandle().GetObject<AudioClipProperties>();
  65. audioClipPlayable.SetVolume(Mathf.Clamp01(m_TrackProperties.volume * audioClipProperties.volume));
  66. audioClipPlayable.SetStereoPan(Mathf.Clamp(m_TrackProperties.stereoPan, -1.0f, 1.0f));
  67. audioClipPlayable.SetSpatialBlend(Mathf.Clamp01(m_TrackProperties.spatialBlend));
  68. }
  69. tree.Add(new ScheduleRuntimeClip(c, source, clipBlender, buffer));
  70. graph.Connect(source, 0, clipBlender, i);
  71. source.SetSpeed(c.timeScale);
  72. source.SetDuration(c.extrapolatedDuration);
  73. clipBlender.SetInputWeight(source, 1.0f);
  74. }
  75. ConfigureTrackAnimation(tree, go, clipBlender);
  76. return clipBlender;
  77. }
  78. /// <inheritdoc/>
  79. public override IEnumerable<PlayableBinding> outputs
  80. {
  81. get { yield return AudioPlayableBinding.Create(name, this); }
  82. }
  83. #if UNITY_EDITOR
  84. internal void LiveLink()
  85. {
  86. if (!m_LiveMixerPlayable.IsValid())
  87. return;
  88. var audioMixerProperties = m_LiveMixerPlayable.GetHandle().GetObject<AudioMixerProperties>();
  89. if (audioMixerProperties == null)
  90. return;
  91. audioMixerProperties.volume = m_TrackProperties.volume;
  92. audioMixerProperties.stereoPan = m_TrackProperties.stereoPan;
  93. audioMixerProperties.spatialBlend = m_TrackProperties.spatialBlend;
  94. }
  95. #endif
  96. void OnValidate()
  97. {
  98. m_TrackProperties.volume = Mathf.Clamp01(m_TrackProperties.volume);
  99. m_TrackProperties.stereoPan = Mathf.Clamp(m_TrackProperties.stereoPan, -1.0f, 1.0f);
  100. m_TrackProperties.spatialBlend = Mathf.Clamp01(m_TrackProperties.spatialBlend);
  101. }
  102. }
  103. }