Skip to content

Commit 132c3ff

Browse files
committed
Audio Monitor Window: add listener distance.
AudioSourcePlayer: don't play sound on enable if there is none specified
1 parent 5a43833 commit 132c3ff

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

Assets/DevLocker/Audio/AudioSourcePlayer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ protected virtual void OnEnable()
192192
m_ActivePlayersRegister.Add(this);
193193

194194
AudioSource.enabled = true;
195-
if (PlayOnEnable) {
195+
if (PlayOnEnable && AudioResource) {
196196
Play();
197197
}
198198
}

Assets/DevLocker/Audio/Editor/AudioSourcePlayerMonitorWindow.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ private struct ActionEntry
4242
public float Volume;
4343
public float Pitch;
4444
public float SpatialBlend;
45+
46+
public float ListenerDistance;
4547
}
4648

4749
private bool m_ListenForEvents = true;
@@ -55,6 +57,8 @@ private struct ActionEntry
5557

5658
private Vector2 m_ScrollView;
5759

60+
private AudioListener m_AudioListener;
61+
5862
public static void ShowMonitor()
5963
{
6064
var window = GetWindow<AudioSourcePlayerMonitorWindow>(false, "Audio Monitor");
@@ -107,6 +111,10 @@ private void AddAction(ActionType actionType, AudioSourcePlayer player)
107111
if (!m_ListenForEvents)
108112
return;
109113

114+
if (m_AudioListener == null || !m_AudioListener.isActiveAndEnabled) {
115+
m_AudioListener = GameObject.FindAnyObjectByType<AudioListener>();
116+
}
117+
110118
m_Actions.Add(new ActionEntry() {
111119
Type = actionType,
112120
Time = Time.time,
@@ -124,6 +132,7 @@ private void AddAction(ActionType actionType, AudioSourcePlayer player)
124132
Pitch = player.Pitch,
125133
SpatialBlend = player.SpatialBlend,
126134

135+
ListenerDistance = m_AudioListener ? Vector3.Distance(m_AudioListener.transform.position, player.transform.position) : -1f,
127136
});
128137

129138
if (m_Actions.Count > m_EntriesLimit) {
@@ -156,6 +165,7 @@ private void OnUIAudioEffectsPlayed(UIAudioEffects uiAudioEffects, AudioResource
156165
Pitch = uiAudioEffects.AudioSource?.pitch ?? 1f,
157166
SpatialBlend = uiAudioEffects.AudioSource?.spatialBlend ?? 0f,
158167

168+
ListenerDistance = -1f,
159169
});
160170

161171
if (m_Actions.Count > m_EntriesLimit) {
@@ -189,17 +199,19 @@ void OnGUI()
189199

190200
GUI.backgroundColor = prevBackgroundColor;
191201

192-
m_EntriesLimit = Mathf.Max(0, EditorGUILayout.IntField("Limit Entries", m_EntriesLimit, GUILayout.ExpandWidth(false)));
193-
194202
GUI.backgroundColor = m_ShowDetails ? Color.green : prevBackgroundColor;
195203
if (GUILayout.Button("Details", GUILayout.ExpandWidth(false))) {
196204
m_ShowDetails = !m_ShowDetails;
197205
}
198206
GUI.backgroundColor = prevBackgroundColor;
199207

208+
float prevLabelWidth = EditorGUIUtility.labelWidth;
209+
EditorGUIUtility.labelWidth = 80f;
210+
m_EntriesLimit = Mathf.Max(0, EditorGUILayout.IntField("Limit Entries", m_EntriesLimit, GUILayout.Width(112f)));
211+
EditorGUIUtility.labelWidth = prevLabelWidth;
212+
200213
GUILayout.FlexibleSpace();
201214

202-
float prevLabelWidth = EditorGUIUtility.labelWidth;
203215
EditorGUIUtility.labelWidth = 80f;
204216
m_ClearOnPlay = EditorGUILayout.Toggle("Clear on play", m_ClearOnPlay);
205217
EditorGUIUtility.labelWidth = prevLabelWidth;
@@ -228,10 +240,11 @@ private void DrawSimpleView()
228240
EditorGUILayout.BeginHorizontal();
229241
{
230242
GUILayout.Label("Action", HeaderStyle, GUILayout.Width(enumColumnWidth));
231-
GUILayout.Label("Time", HeaderStyle, GUILayout.Width(timeColumnWidth));
243+
GUILayout.Label("Time", HeaderStyle, GUILayout.MaxWidth(timeColumnWidth));
232244

233245
GUILayout.Label("Player", HeaderStyle, GUILayout.ExpandWidth(true));
234246
GUILayout.Label("Resource", HeaderStyle, GUILayout.ExpandWidth(true));
247+
GUILayout.Label("Distance", HeaderStyle, GUILayout.MaxWidth(timeColumnWidth));
235248
}
236249
EditorGUILayout.EndHorizontal();
237250

@@ -249,9 +262,10 @@ private void DrawSimpleView()
249262
EditorGUILayout.BeginHorizontal();
250263
{
251264
GUILayout.Label(action.Type.ToString(), EditorStyles.boldLabel, GUILayout.Width(enumColumnWidth));
252-
EditorGUILayout.FloatField(action.Time, GUILayout.Width(timeColumnWidth));
265+
EditorGUILayout.FloatField(action.Time, GUILayout.MaxWidth(timeColumnWidth));
253266
EditorGUILayout.ObjectField(action.Player, action.Player?.GetType(), true, GUILayout.ExpandWidth(true));
254267
EditorGUILayout.ObjectField(action.Resource, audioType, true, GUILayout.ExpandWidth(true));
268+
EditorGUILayout.FloatField(action.ListenerDistance, GUILayout.MaxWidth(timeColumnWidth));
255269

256270
}
257271
EditorGUILayout.EndHorizontal();

0 commit comments

Comments
 (0)