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