public override void OnInspectorGUI() { var groups = Manager.Base.Sounds.Where(s => s.Type.Contains('/')).GroupBy(e => e.Type.Split('/').First()).Select(e => e.Key); serializedObject.Update(); var audioBase = serializedObject.FindProperty("Base"); var audioBaseSO = new SerializedObject(audioBase.objectReferenceValue); audioBaseSO.Update(); var sounds = audioBaseSO.FindProperty("Sounds"); EditorGUILayout.PropertyField(audioBase); EditorGUILayout.Space(); if (GUILayout.Toggle(_unfoldedGroup == string.Empty, "All", EditorStyles.toolbarButton)) { _unfoldedGroup = string.Empty; } foreach (var g in groups) { if (GUILayout.Toggle(_unfoldedGroup == g, g, EditorStyles.toolbarButton) && _unfoldedGroup != g) { _unfoldedGroup = g; _unfoldedSound = string.Empty; } } EditorGUILayout.Space(); int toReplace = -1; var soundsArray = AudioManagerUtils.AsArray(sounds); for (int index = 0; index < soundsArray.Length; index++) { var sound = soundsArray[index]; var soundType = sound.FindPropertyRelative("Type"); var clips = sound.FindPropertyRelative("Clips"); var clipsArray = AudioManagerUtils.AsArray(clips); bool toDraw = !soundType.stringValue.Contains('/') || soundType.stringValue.StartsWith(_unfoldedGroup); if (!toDraw) { continue; } bool unfolded = soundType.stringValue == _unfoldedSound; AudioManagerUtils.DrawLine(AudioManagerUtils.Blue); if (clipsArray.Length == 0 || clipsArray.Any(c => c.objectReferenceValue == null)) { AudioManagerUtils.DrawBackgroundBox(AudioManagerUtils.Red, 28, -4); } if (clipsArray.Length > 1) { AudioManagerUtils.DrawBackgroundBox(AudioManagerUtils.Yellow, 28, -4); } using (new GUILayout.HorizontalScope()) { if (GUILayout.Button(unfolded ? AudioManagerUtils.ArrowUp : AudioManagerUtils.ArrowDown, EditorStyles.toolbarButton, GUILayout.Width(20))) { _unfoldedSound = unfolded ? string.Empty : soundType.stringValue; } var newType = EditorGUILayout.DelayedTextField(soundType.stringValue); if (soundType.stringValue != newType) { soundType.stringValue = newType; if (newType.Contains('/')) { _unfoldedGroup = newType.Split('/')[0]; } } // Replace clips of Sound with new ones var newClipsToReplace = AudioManagerUtils.DropArea <AudioClip>("replace", 20); if (newClipsToReplace != null) { AudioManagerUtils.ReplaceArray(clips, newClipsToReplace.ToArray <Object>()); } if (unfolded) { if (GUILayout.Button("x", EditorStyles.toolbarButton, GUILayout.Width(20))) { toReplace = index; } } } // Draw Clips of Sound if unfolded if (_unfoldedSound != soundType.stringValue) { continue; } EditorGUI.indentLevel++; for (int y = 0; y < clipsArray.Length; y++) { clipsArray[y].objectReferenceValue = (AudioClip)EditorGUILayout.ObjectField(clipsArray[y].objectReferenceValue, typeof(AudioClip), false); } EditorGUI.indentLevel--; } EditorGUILayout.Space(); var newClips = AudioManagerUtils.DropArea <AudioClip>("Drop to add sound", 50); if (newClips != null) { var newSound = AudioManagerUtils.NewElement(sounds); var newSoundType = newSound.FindPropertyRelative("Type"); var newSoundId = newSound.FindPropertyRelative("Id"); var newSoundClips = newSound.FindPropertyRelative("Clips"); newSoundType.stringValue = string.IsNullOrEmpty(_unfoldedGroup) ? "Custom" : _unfoldedGroup; newSoundId.stringValue = Guid.NewGuid().ToString(); AudioManagerUtils.ReplaceArray(newSoundClips, newClips.ToArray <Object>()); } EditorGUILayout.Space(); if (toReplace >= 0) { sounds.DeleteArrayElementAtIndex(toReplace); } if (GUI.changed) { EditorUtility.SetDirty(Manager); serializedObject.ApplyModifiedProperties(); audioBaseSO.ApplyModifiedProperties(); } }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { if (_manager == null) { _manager = Object.FindObjectOfType <AudioManager>(); } if (_manager == null) { EditorGUI.LabelField(position, label.text + ": AudioManager required on scene to draw gui", EditorStyles.boldLabel); EditorGUI.PropertyField(position, property, label, true); return; } SerializedProperty type = property.FindPropertyRelative("Type"); SerializedProperty id = property.FindPropertyRelative("Id"); SerializedProperty clips = property.FindPropertyRelative("Clips"); var labelWidth = EditorGUIUtility.labelWidth; var width = position.width; bool custom = type.stringValue == "Custom"; if (id.stringValue == null) { id.stringValue = string.Empty; } EditorGUI.BeginProperty(position, label, property); bool customValid = custom && clips.arraySize > 0; if (customValid) { if (clips.arraySize == 0) { customValid = false; } else { var firstClipProp = clips.GetArrayElementAtIndex(0); var firstClip = firstClipProp.objectReferenceValue as AudioClip; customValid = firstClip != null; } } bool soundValid = false; if (!custom) { if (clips.arraySize > 0) { clips.arraySize = 0; clips.serializedObject.ApplyModifiedProperties(); } var soundInBase = !string.IsNullOrEmpty(id.stringValue) ? _manager.Base.Sounds.SingleOrDefault(s => s.Id == id.stringValue) : null; if (soundInBase == null) { soundInBase = _manager.Base.Sounds.SingleOrDefault(s => s.Type == type.stringValue); } if (soundInBase != null) { if (soundInBase.Id != id.stringValue) { if (!string.IsNullOrEmpty(id.stringValue)) { Debug.LogWarning("Sound Id changed from " + id.stringValue + " to " + soundInBase.Id, property.serializedObject.targetObject); } id.stringValue = soundInBase.Id; id.serializedObject.ApplyModifiedProperties(); } if (soundInBase.Type != type.stringValue) { Debug.LogWarning("Sound Type changed from " + type.stringValue + " to " + soundInBase.Type, property.serializedObject.targetObject); type.stringValue = soundInBase.Type; type.serializedObject.ApplyModifiedProperties(); } soundValid = true; } } bool valid = (custom && customValid) || soundValid; var colored = position; colored.y += 5; colored.height -= 8; AudioManagerUtils.DrawColouredRect(colored, valid ? AudioManagerUtils.Blue : AudioManagerUtils.Red); position.y += 10; position.height = 20; position.width = labelWidth - 5; EditorGUI.LabelField(position, label); position.x += labelWidth; if (custom) { position.width = 20; } else { position.width = width - labelWidth - 5; } if (GUI.Button(position, custom ? "~" : type.stringValue, EditorStyles.toolbarButton)) { AudioManagerEditor.DrawMenu(data => { type.stringValue = (string)data; if (type.stringValue != "Custom") { id.stringValue = _manager.Base.Sounds.Single(s => s.Type == type.stringValue).Id; } property.serializedObject.ApplyModifiedProperties(); }); } if (custom) { position.width = width - (labelWidth + 25); position.x = labelWidth + 40; position.height = 16; position.y += 1; Object clip = null; if (clips.arraySize > 0) { clip = clips.GetArrayElementAtIndex(0).objectReferenceValue; if (clip == null) { clips.arraySize = 0; clips.serializedObject.ApplyModifiedProperties(); } } var newClip = EditorGUI.ObjectField(position, clip, typeof(AudioClip), false); if (clip != newClip) { if (clips.arraySize < 1) { clips.arraySize = 1; clips.serializedObject.ApplyModifiedProperties(); } clips.GetArrayElementAtIndex(0).objectReferenceValue = newClip; } } EditorGUI.EndProperty(); if (property.depth == 0) { Sound sound = fieldInfo.GetValue(property.serializedObject.targetObject) as Sound; if (sound != null && clips.arraySize == 0 && sound.Clips != null) { sound.Clips = null; fieldInfo.SetValue(property.serializedObject.targetObject, sound); EditorUtility.SetDirty(property.serializedObject.targetObject); } } if (GUI.changed) { EditorUtility.SetDirty(property.serializedObject.targetObject); property.serializedObject.ApplyModifiedProperties(); } }
public override void OnInspectorGUI() { var themeAmbient = serializedObject.FindProperty("_ambient"); var themeRandoms = serializedObject.FindProperty("_randomSounds"); var themeVolume = serializedObject.FindProperty("_baseVolume"); AudioManagerUtils.DrawLine(AudioManagerUtils.Gray); EditorGUILayout.PropertyField(themeAmbient); bool addRandom = false; int removeRandom = -1; if (GUILayout.Button("Add Random Sound", EditorStyles.toolbarButton)) { addRandom = true; } var randoms = AudioManagerUtils.AsArray(themeRandoms); for (int i = 0; i < randoms.Length; i++) { var random = randoms[i]; var sound = random.FindPropertyRelative("Sound"); var minDelay = random.FindPropertyRelative("MinSecondsDelay"); var maxDelay = random.FindPropertyRelative("MaxSecondsDelay"); EditorGUILayout.PropertyField(sound); using (new GUILayout.HorizontalScope()) { if (GUILayout.Button("-", EditorStyles.toolbarButton, GUILayout.Width(25))) { removeRandom = i; } EditorGUILayout.Space(); EditorGUILayout.PropertyField(minDelay, GUIContent.none, GUILayout.Width(60)); EditorGUILayout.PropertyField(maxDelay, GUIContent.none, GUILayout.Width(60)); EditorGUILayout.Space(); } if (minDelay.floatValue < 10) { minDelay.floatValue = 10; } if (maxDelay.floatValue < minDelay.floatValue) { maxDelay.floatValue = minDelay.floatValue + 5; } AudioManagerUtils.DrawLine(AudioManagerUtils.Green); } EditorGUILayout.PropertyField(themeVolume); AudioManagerUtils.DrawLine(AudioManagerUtils.Gray); if (addRandom) { AudioManagerUtils.NewElement(themeRandoms); } if (removeRandom >= 0) { themeRandoms.DeleteArrayElementAtIndex(removeRandom); } if (GUI.changed) { serializedObject.ApplyModifiedProperties(); } }