AudioTrack.cs 4.4 KB

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