Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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