using UnityEngine; using UnityEditor; using System.Collections; using System.Collections.Generic; using System; [Flags] public enum EEditorListOption { None = 0, ListSize = 1, ListLabel = 2, ElementLabels = 4, Buttons = 8, Default = ListSize | ListLabel | ElementLabels, NoElementLabels = ListSize | ListLabel, All = Default | Buttons } public static class EditorList { private static GUIContent moveButtonContent = new GUIContent("\u21b4", "move down"), duplicateButtonContent = new GUIContent("+", "duplicate"), deleteButtonContent = new GUIContent("-", "delete"), addButtonContent = new GUIContent("+", "add element"); public static void Show(SerializedProperty list, EEditorListOption options = EEditorListOption.Default) { bool showListLabel = (options & EEditorListOption.ListLabel) != 0, showListSize = (options & EEditorListOption.ListSize) != 0; if (showListLabel) { EditorGUILayout.PropertyField(list); EditorGUI.indentLevel += 1; } if (!showListLabel || list.isExpanded) { if (showListSize) { EditorGUILayout.PropertyField(list.FindPropertyRelative("Array.size")); } ShowElements(list, options); } if (showListLabel) { EditorGUI.indentLevel -= 1; } } private static void ShowElements(SerializedProperty list, EEditorListOption options) { bool showElementLabels = (options & EEditorListOption.ElementLabels) != 0, showButtons = (options & EEditorListOption.Buttons) != 0; for (int i = 0; i < list.arraySize; i++) { if (showButtons) { EditorGUILayout.BeginHorizontal(); } var element = list.GetArrayElementAtIndex(i); if (showElementLabels) { EditorGUILayout.PropertyField(element, new GUIContent(element.FindPropertyRelative("name").stringValue), true); } else { EditorGUILayout.PropertyField(element, GUIContent.none, true); } if (showButtons) { ShowButtons(list, i); EditorGUILayout.EndHorizontal(); } } if (showButtons && list.arraySize == 0 && GUILayout.Button(addButtonContent, EditorStyles.miniButton)) { list.arraySize += 1; } } private static GUILayoutOption miniButtonWidth = GUILayout.Width(20f); private static void ShowButtons(SerializedProperty list, int index) { if (GUILayout.Button(moveButtonContent, EditorStyles.miniButtonLeft, miniButtonWidth)) { list.MoveArrayElement(index, index + 1); } if (GUILayout.Button(duplicateButtonContent, EditorStyles.miniButtonMid, miniButtonWidth)) { list.InsertArrayElementAtIndex(index); } if (GUILayout.Button(deleteButtonContent, EditorStyles.miniButtonRight, miniButtonWidth)) { int oldSize = list.arraySize; list.DeleteArrayElementAtIndex(index); if (list.arraySize == oldSize) { list.DeleteArrayElementAtIndex(index); } } } }