using System; using UnityEditor; using UnityEditorInternal; using UnityEngine; [CustomEditor(typeof(EventTrigger))] public class EventTriggerEditor : Editor { ReorderableList commandList; void OnEnable() { commandList = new ReorderableList(serializedObject, serializedObject.FindProperty("commands"), true, true, true, true); commandList.elementHeightCallback = (index) => { var element = commandList.serializedProperty.GetArrayElementAtIndex(index); return GetElementHeight((CommandType)element.FindPropertyRelative("commandType").enumValueIndex); }; commandList.drawElementCallback = (rect, index, isActive, isFocused) => { var element = commandList.serializedProperty.GetArrayElementAtIndex(index); rect.y += 2; EditorGUI.PropertyField(new Rect(rect.x, rect.y, 60, EditorGUIUtility.singleLineHeight), element.FindPropertyRelative("commandType"), GUIContent.none); CommandType commandType = (CommandType)element.FindPropertyRelative("commandType").enumValueIndex; SerializedProperty commandArguments = element.FindPropertyRelative("commandArguments"); switch (commandType) { case CommandType.Teleport: if (commandArguments.arraySize < 1) { commandArguments.arraySize = 1; } EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight), commandArguments.GetArrayElementAtIndex(0), new GUIContent("Target")); break; case CommandType.Write: if (commandArguments.arraySize < 2) { commandArguments.arraySize = 2; } EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight), commandArguments.GetArrayElementAtIndex(0), new GUIContent("Write Speed")); EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight), commandArguments.GetArrayElementAtIndex(1), new GUIContent("Content")); break; case CommandType.Move: if (commandArguments.arraySize < 1) { commandArguments.arraySize = 1; } EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight), commandArguments.GetArrayElementAtIndex(0), new GUIContent("Target")); break; case CommandType.SendEvent: if (commandArguments.arraySize < 3) { commandArguments.arraySize = 3; } EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight), commandArguments.GetArrayElementAtIndex(0), new GUIContent("TargetID")); EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight), commandArguments.GetArrayElementAtIndex(1), new GUIContent("Start Delay")); SerializedProperty nestedCommands = element.FindPropertyRelative("nestedCommands"); for (int j = 0; j < nestedCommands.arraySize; j++) { SerializedProperty nestedCommand = nestedCommands.GetArrayElementAtIndex(j); SerializedProperty nestedCommandType = nestedCommand.FindPropertyRelative("commandType"); SerializedProperty nestedCommandArguments = nestedCommand.FindPropertyRelative("commandArguments"); rect.y += EditorGUIUtility.singleLineHeight * 2; EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight), nestedCommandType, GUIContent.none); CommandType nestedCommandTypeValue = (CommandType)nestedCommandType.enumValueIndex; switch (nestedCommandTypeValue) { case CommandType.Teleport: if (nestedCommandArguments.arraySize < 1) { nestedCommandArguments.arraySize = 1; } EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight), nestedCommandArguments.GetArrayElementAtIndex(0), new GUIContent("Target")); break; case CommandType.Write: if (nestedCommandArguments.arraySize < 2) { nestedCommandArguments.arraySize = 2; } EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight), nestedCommandArguments.GetArrayElementAtIndex(0), new GUIContent("Write Speed")); EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight * 2, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight), nestedCommandArguments.GetArrayElementAtIndex(1), new GUIContent("Content")); break; case CommandType.Move: if (nestedCommandArguments.arraySize < 1) { nestedCommandArguments.arraySize = 1; } EditorGUI.PropertyField(new Rect(rect.x + 60, rect.y + EditorGUIUtility.singleLineHeight, rect.width - 60 - 30, EditorGUIUtility.singleLineHeight), nestedCommandArguments.GetArrayElementAtIndex(0), new GUIContent("Target")); break; } } if (GUI.Button(new Rect(rect.x + rect.width - 60, rect.y + EditorGUIUtility.singleLineHeight * (nestedCommands.arraySize + 2), 30, EditorGUIUtility.singleLineHeight), "+")) { nestedCommands.arraySize++; } if (nestedCommands.arraySize > 0 && GUI.Button(new Rect(rect.x + rect.width - 30, rect.y + EditorGUIUtility.singleLineHeight * (nestedCommands.arraySize + 2), 30, EditorGUIUtility.singleLineHeight), "-")) { nestedCommands.arraySize--; } break; } }; } public override void OnInspectorGUI() { serializedObject.Update(); commandList.DoLayoutList(); serializedObject.ApplyModifiedProperties(); } float GetElementHeight(CommandType commandType) { float height = 0; switch (commandType) { case CommandType.Teleport: height = 1; break; case CommandType.Write: height = 2; break; case CommandType.Move: height = 1; break; case CommandType.SendEvent: height = 6; break; } if (commandType == CommandType.SendEvent) { height += 1; } return height * EditorGUIUtility.singleLineHeight + 2; } }