3 using System.Collections.Generic;
16 private string[] eventNames;
17 private int selectedEventIndex = 0;
18 private readonly
string[] posTypes = {
"2D",
"3D",
"Spatial Sound" };
19 private Rect editorCurveSize =
new Rect(0f, 0f, 1f, 1f);
24 if (this.MyTarget.
Events == null)
26 this.MyTarget.
Events =
new TEvent[0];
28 this.eventNames =
new string[this.MyTarget.
Events.Length];
29 UpdateEventNames(this.MyTarget.
Events);
34 this.serializedObject.Update();
35 EditorGUI.BeginChangeCheck();
36 DrawEventHeader(this.MyTarget.
Events);
38 if (this.MyTarget.
Events != null &&
this.MyTarget.Events.Length > 0)
41 EditorGUI.indentLevel++;
42 this.selectedEventIndex = EditorGUILayout.Popup(this.selectedEventIndex, this.eventNames);
44 if (this.selectedEventIndex < this.MyTarget.
Events.Length)
48 selectedEvent = this.MyTarget.
Events[this.selectedEventIndex];
49 SerializedProperty selectedEventProperty = this.serializedObject.FindProperty(
"Events.Array.data[" + this.selectedEventIndex.ToString() +
"]");
50 EditorGUILayout.Space();
52 if (selectedEventProperty != null)
54 DrawEventInspector(selectedEventProperty, selectedEvent, this.MyTarget.
Events, showEmitters);
55 if (!DrawContainerInspector(selectedEventProperty, selectedEvent))
57 EditorGUI.indentLevel++;
58 DrawSoundClipInspector(selectedEventProperty, selectedEvent);
59 EditorGUI.indentLevel--;
63 EditorGUI.indentLevel--;
67 EditorGUI.EndChangeCheck();
68 this.serializedObject.ApplyModifiedProperties();
72 EditorUtility.SetDirty(this.MyTarget);
76 private void DrawEventHeader(TEvent[] EditorEvents)
79 EditorGUILayout.Space();
80 EditorGUILayout.BeginHorizontal();
83 using (
new EditorGUI.DisabledScope((EditorEvents != null) && (EditorEvents.Length < 1)))
87 this.MyTarget.
Events = RemoveAudioEvent(EditorEvents, this.selectedEventIndex);
93 this.MyTarget.
Events = AddAudioEvent(EditorEvents);
96 EditorGUILayout.EndHorizontal();
97 EditorGUILayout.Space();
100 private void DrawEventInspector(SerializedProperty selectedEventProperty, TEvent selectedEvent, TEvent[] EditorEvents,
bool showEmitters)
103 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"Name"));
105 if (selectedEvent.Name !=
this.eventNames[
this.selectedEventIndex])
107 UpdateEventNames(EditorEvents);
112 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"PrimarySource"));
113 if (selectedEvent.IsContinuous())
115 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"SecondarySource"));
120 selectedEvent.Spatialization = (
SpatialPositioningType)EditorGUILayout.Popup(
"Positioning", (
int)selectedEvent.Spatialization,
this.posTypes);
124 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"RoomSize"));
126 EditorGUILayout.Space();
130 float curveHeight = 30f;
131 float curveWidth = 300f;
134 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"MaxDistanceAttenuation3D"));
137 SerializedProperty attenuationProperty = selectedEventProperty.FindPropertyRelative(
"AttenuationCurve");
138 SerializedProperty spatialProperty = selectedEventProperty.FindPropertyRelative(
"SpatialCurve");
139 SerializedProperty spreadProperty = selectedEventProperty.FindPropertyRelative(
"SpreadCurve");
141 SerializedProperty reverbProperty = selectedEventProperty.FindPropertyRelative(
"ReverbCurve");
144 EditorGUILayout.CurveField(attenuationProperty, Color.red, editorCurveSize,
new GUIContent(
"Attenuation"), GUILayout.Height(curveHeight), GUILayout.Width(curveWidth), GUILayout.ExpandHeight(
false), GUILayout.ExpandWidth(
true));
149 EditorGUILayout.CurveField(spatialProperty, Color.green, editorCurveSize,
new GUIContent(
"Spatial"), GUILayout.Height(curveHeight), GUILayout.Width(curveWidth), GUILayout.ExpandHeight(
false), GUILayout.ExpandWidth(
true));
151 EditorGUILayout.CurveField(spreadProperty, Color.blue, editorCurveSize,
new GUIContent(
"Spread"), GUILayout.Height(curveHeight), GUILayout.Width(curveWidth), GUILayout.ExpandHeight(
false), GUILayout.ExpandWidth(
true));
155 EditorGUILayout.CurveField(reverbProperty, Color.yellow, editorCurveSize,
new GUIContent(
"Reverb"), GUILayout.Height(curveHeight), GUILayout.Width(curveWidth), GUILayout.ExpandHeight(
false), GUILayout.ExpandWidth(
true));
158 EditorGUILayout.Space();
162 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"AudioBus"));
165 if (!selectedEvent.IsContinuous())
167 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"FadeInTime"));
168 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"FadeOutTime"));
172 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"PitchCenter"));
175 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"VolumeCenter"));
180 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"PanCenter"));
184 EditorGUILayout.BeginHorizontal();
185 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"InstanceLimit"));
186 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"InstanceTimeBuffer"));
187 EditorGUILayout.EndHorizontal();
188 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"AudioEventInstanceBehavior"));
191 EditorGUILayout.Space();
194 private bool DrawContainerInspector(SerializedProperty selectedEventProperty, TEvent selectedEvent)
196 bool addedSound =
false;
197 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"Container.ContainerType"));
199 if (!selectedEvent.IsContinuous())
201 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"Container.Looping"));
203 if (selectedEvent.Container.Looping)
205 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"Container.LoopTime"));
210 EditorGUILayout.Space();
212 if (selectedEvent.IsContinuous())
214 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"Container.CrossfadeTime"));
217 EditorGUILayout.BeginHorizontal();
218 EditorGUILayout.LabelField(
"Sounds");
222 AddSound(selectedEvent);
227 EditorGUILayout.EndHorizontal();
231 private void DrawSoundClipInspector(SerializedProperty selectedEventProperty, TEvent selectedEvent)
233 bool allowLoopingClip = !selectedEvent.Container.Looping;
235 if (allowLoopingClip)
237 if (selectedEvent.IsContinuous())
239 allowLoopingClip =
false;
243 for (
int i = 0; i < selectedEvent.Container.Sounds.Length; i++)
245 EditorGUILayout.Space();
246 EditorGUILayout.BeginHorizontal();
247 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"Container.Sounds.Array.data[" + i +
"].Sound"));
251 selectedEventProperty.FindPropertyRelative(
"Container.Sounds.Array.data[" + i +
"]").DeleteCommand();
255 EditorGUILayout.EndHorizontal();
257 if (!selectedEvent.IsContinuous())
259 EditorGUILayout.BeginHorizontal();
260 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"Container.Sounds.Array.data[" + i +
"].DelayCenter"));
261 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"Container.Sounds.Array.data[" + i +
"].DelayRandomization"));
262 EditorGUILayout.EndHorizontal();
265 if (allowLoopingClip)
267 EditorGUILayout.PropertyField(selectedEventProperty.FindPropertyRelative(
"Container.Sounds.Array.data[" + i +
"].Looping"));
269 if (selectedEvent.Container.Sounds[i].Looping && selectedEvent.Container.ContainerType ==
AudioContainerType.Simultaneous)
271 allowLoopingClip =
false;
276 selectedEvent.Container.Sounds[i].Looping =
false;
282 private void UpdateEventNames(TEvent[] EditorEvents)
284 HashSet<string> previousEventNames =
new HashSet<string>();
286 for (
int i = 0; i < EditorEvents.Length; i++)
288 if (
string.IsNullOrEmpty(EditorEvents[i].Name))
290 EditorEvents[i].Name =
"_NewEvent" + i.ToString();
293 while (previousEventNames.Contains(EditorEvents[i].Name))
295 EditorEvents[i].Name =
"_" + EditorEvents[i].Name;
298 this.eventNames[i] = EditorEvents[i].Name;
299 previousEventNames.Add(this.eventNames[i]);
303 private void AddSound(TEvent selectedEvent)
307 selectedEvent.Container.Sounds.CopyTo(tempClips, 0);
308 tempClips[tempClips.Length - 1] =
new UAudioClip();
309 selectedEvent.Container.Sounds = tempClips;
312 private TEvent[] AddAudioEvent(TEvent[] EditorEvents)
314 TEvent tempEvent =
new TEvent();
315 TEvent[] tempEventArray =
new TEvent[EditorEvents.Length + 1];
317 tempEvent.Container.Sounds =
new UAudioClip[0];
318 EditorEvents.CopyTo(tempEventArray, 0);
319 tempEventArray[EditorEvents.Length] = tempEvent;
320 this.eventNames =
new string[tempEventArray.Length];
321 UpdateEventNames(tempEventArray);
322 this.selectedEventIndex = this.eventNames.Length - 1;
323 return tempEventArray;
326 private TEvent[] RemoveAudioEvent(TEvent[] editorEvents,
int eventToRemove)
328 editorEvents = RemoveElement(editorEvents, eventToRemove);
329 this.eventNames =
new string[editorEvents.Length];
330 UpdateEventNames(editorEvents);
332 if (this.selectedEventIndex >= editorEvents.Length)
334 this.selectedEventIndex--;
346 public static T[] RemoveElement<T>(T[] array,
int index)
348 T[] newArray =
new T[array.Length - 1];
350 for (
int i = 0; i < array.Length; i++)
355 newArray[i - 1] = array[i];
359 newArray[i] = array[i];
370 private void OnEnable()
378 DrawInspectorGUI(
false);