AudioTrackInspector.cs
7.45 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.Timeline;
using UnityEngine.Playables;
namespace UnityEngine.Timeline
{
[CustomEditor(typeof(AudioTrack))]
[CanEditMultipleObjects]
class AudioTrackInspector : TrackAssetInspector
{
[UsedImplicitly] // Also used by tests
internal static class Styles
{
public const string VolumeControl = "AudioTrackInspector.volume";
public const string StereoPanControl = "AudioTrackInspector.stereoPan";
public const string SpatialBlendControl = "AudioTrackInspector.spatialBlend";
const string k_Indent = " ";
public const string valuesFormatter = "0.###";
public const string mixInfoSectionSeparator = "\n\n";
public static string mixedPropertiesInfo = L10n.Tr("The final {3} is {0}\n" +
"Calculated from:\n" +
k_Indent + "Track: {1}\n" +
k_Indent + "AudioSource: {2}");
}
static StringBuilder s_MixInfoBuilder = new StringBuilder();
SerializedProperty m_VolumeProperty;
SerializedProperty m_StereoPanProperty;
SerializedProperty m_SpatialBlendProperty;
PlayableDirector m_Director;
public override void OnEnable()
{
base.OnEnable();
if (((AudioTrack)target).timelineAsset == TimelineEditor.inspectedAsset)
m_Director = TimelineEditor.inspectedDirector;
m_VolumeProperty = serializedObject.FindProperty("m_TrackProperties.volume");
m_StereoPanProperty = serializedObject.FindProperty("m_TrackProperties.stereoPan");
m_SpatialBlendProperty = serializedObject.FindProperty("m_TrackProperties.spatialBlend");
}
protected override void DrawTrackProperties()
{
// Volume
GUI.SetNextControlName(Styles.VolumeControl);
EditorGUILayout.Slider(m_VolumeProperty, 0.0f, 1.0f, AudioSourceInspector.Styles.volumeLabel);
EditorGUILayout.Space();
// Stereo Pan
GUI.SetNextControlName(Styles.StereoPanControl);
EditorGUIUtility.sliderLabels.SetLabels(AudioSourceInspector.Styles.panLeftLabel, AudioSourceInspector.Styles.panRightLabel);
EditorGUILayout.Slider(m_StereoPanProperty, -1.0f, 1.0f, AudioSourceInspector.Styles.panStereoLabel);
EditorGUIUtility.sliderLabels.SetLabels(null, null);
EditorGUILayout.Space();
// Spatial Blend
using (new EditorGUI.DisabledScope(ShouldDisableSpatialBlend()))
{
GUI.SetNextControlName(Styles.SpatialBlendControl);
EditorGUIUtility.sliderLabels.SetLabels(AudioSourceInspector.Styles.spatialLeftLabel, AudioSourceInspector.Styles.spatialRightLabel);
EditorGUILayout.Slider(m_SpatialBlendProperty, 0.0f, 1.0f, AudioSourceInspector.Styles.spatialBlendLabel);
EditorGUIUtility.sliderLabels.SetLabels(null, null);
}
DrawMixInfoSection();
}
void DrawMixInfoSection()
{
if (m_Director == null || targets.Length > 1)
return;
var binding = m_Director.GetGenericBinding(target) as AudioSource;
if (binding == null)
return;
var audioSourceVolume = binding.volume;
var audioSourcePan = binding.panStereo;
var audioSourceBlend = binding.spatialBlend;
var trackVolume = m_VolumeProperty.floatValue;
var trackPan = m_StereoPanProperty.floatValue;
var trackBlend = m_SpatialBlendProperty.floatValue;
// Skip sections when result is obvious
var skipVolumeInfo = Math.Abs(audioSourceVolume) < float.Epsilon && Math.Abs(trackVolume) < float.Epsilon || // All muted
Math.Abs(audioSourceVolume - 1) < float.Epsilon && Math.Abs(trackVolume - 1) < float.Epsilon; // All max volume
var skipPanInfo = Math.Abs(audioSourcePan) < float.Epsilon && Math.Abs(trackPan) < float.Epsilon || // All centered
Math.Abs(audioSourcePan - 1) < float.Epsilon && Math.Abs(trackPan - 1) < float.Epsilon || // All right
Math.Abs(audioSourcePan - (-1.0f)) < float.Epsilon && Math.Abs(trackPan - (-1.0f)) < float.Epsilon; // All left
var skipBlendInfo = Math.Abs(audioSourceBlend) < float.Epsilon && Math.Abs(trackBlend) < float.Epsilon || // All 2D
Math.Abs(audioSourceBlend - 1) < float.Epsilon && Math.Abs(trackBlend - 1) < float.Epsilon; // All 3D
if (skipVolumeInfo && skipPanInfo && skipBlendInfo)
return;
s_MixInfoBuilder.Length = 0;
if (!skipVolumeInfo)
s_MixInfoBuilder.AppendFormat(
Styles.mixedPropertiesInfo,
(audioSourceVolume * trackVolume).ToString(Styles.valuesFormatter, CultureInfo.InvariantCulture),
trackVolume.ToString(Styles.valuesFormatter, CultureInfo.InvariantCulture),
audioSourceVolume.ToString(Styles.valuesFormatter, CultureInfo.InvariantCulture),
AudioSourceInspector.Styles.volumeLabel.text);
if (!skipVolumeInfo && !skipPanInfo)
s_MixInfoBuilder.Append(Styles.mixInfoSectionSeparator);
if (!skipPanInfo)
s_MixInfoBuilder.AppendFormat(
Styles.mixedPropertiesInfo,
Mathf.Clamp(audioSourcePan + trackPan, -1.0f, 1.0f).ToString(Styles.valuesFormatter, CultureInfo.InvariantCulture),
trackPan.ToString(Styles.valuesFormatter, CultureInfo.InvariantCulture),
audioSourcePan.ToString(Styles.valuesFormatter, CultureInfo.InvariantCulture),
AudioSourceInspector.Styles.panStereoLabel.text);
if ((!skipVolumeInfo || !skipPanInfo) && !skipBlendInfo)
s_MixInfoBuilder.Append(Styles.mixInfoSectionSeparator);
if (!skipBlendInfo)
s_MixInfoBuilder.AppendFormat(
Styles.mixedPropertiesInfo,
Mathf.Clamp01(audioSourceBlend + trackBlend).ToString(Styles.valuesFormatter, CultureInfo.InvariantCulture),
trackBlend.ToString(Styles.valuesFormatter, CultureInfo.InvariantCulture),
audioSourceBlend.ToString(Styles.valuesFormatter, CultureInfo.InvariantCulture),
AudioSourceInspector.Styles.spatialBlendLabel.text);
EditorGUILayout.Space();
EditorGUILayout.HelpBox(new GUIContent(s_MixInfoBuilder.ToString()));
}
protected override void ApplyChanges()
{
var track = (AudioTrack)target;
if (TimelineEditor.inspectedAsset != track.timelineAsset || TimelineEditor.inspectedDirector == null)
return;
if (TimelineEditor.inspectedDirector.state == PlayState.Playing)
track.LiveLink();
else
TimelineEditor.Refresh(RefreshReason.ContentsModified);
}
bool ShouldDisableSpatialBlend()
{
return m_Director == null ||
targets.Any(selectedTrack => m_Director.GetGenericBinding(selectedTrack) == null);
}
}
}