void DrawSection <T>(ref bool show, string header, ref T[] array, DrawArray <T> drawArray, Constructor <T> construct, bool drawAddButton) { show = EditorGUILayout.Foldout(show, header); if (!show) { return; } int itemToDelete = -1; int moveItemIdx = -1; int moveItemDirection = 0; for (int i = 0; i < array.Length; i++) { drawArray(i, ref array, ref itemToDelete, ref moveItemIdx, ref moveItemDirection); GUILayout.Space(5f); } if (itemToDelete != -1) { Undo.RecordObject(specifics, "delete array item"); List <T> list = new List <T>(array as IEnumerable <T>); list.RemoveAt(itemToDelete); array = list.ToArray(); EditorUtility.SetDirty(specifics); } if (moveItemIdx != -1) { int newIdx = moveItemIdx + moveItemDirection; if (newIdx > -1 && newIdx < array.Length) { Undo.RecordObject(specifics, "move array item"); List <T> list = new List <T>(array as IEnumerable <T>); T item = list[moveItemIdx]; list.RemoveAt(moveItemIdx); list.Insert(newIdx, item); array = list.ToArray(); EditorUtility.SetDirty(specifics); } } if (drawAddButton || array == null || array.Length == 0) { GUILayout.BeginHorizontal(); GUILayout.Space(18f); if (GUILayout.Button(string.Empty, addButtonStyle)) { Undo.RecordObject(specifics, "add array item"); List <T> list = new List <T>(array as IEnumerable <T>); list.Add(construct()); array = list.ToArray(); EditorUtility.SetDirty(specifics); } GUILayout.EndHorizontal(); } }
void DrawSection <T>(ref bool show, string header, ref T[] array, DrawArray <T> drawArray, Constructor <T> construct, AddAll <T> addAll) { show = EditorGUILayout.Foldout(show, header); if (!show) { return; } int itemToDelete = -1; int moveItemIdx = -1; int moveItemDirection = 0; for (int i = 0; i < array.Length; i++) { drawArray(i, ref array, ref itemToDelete, ref moveItemIdx, ref moveItemDirection); GUILayout.Space(5f); } if (itemToDelete != -1) { Undo.RegisterUndo(specifics, "delete array item"); List <T> list = new List <T>(array as IEnumerable <T>); list.RemoveAt(itemToDelete); array = list.ToArray(); EditorUtility.SetDirty(specifics); } if (moveItemIdx != -1) { int newIdx = moveItemIdx + moveItemDirection; if (newIdx > -1 && newIdx < array.Length) { Undo.RegisterUndo(specifics, "move array item"); List <T> list = new List <T>(array as IEnumerable <T>); T item = list[moveItemIdx]; list.RemoveAt(moveItemIdx); list.Insert(newIdx, item); array = list.ToArray(); EditorUtility.SetDirty(specifics); } } GUILayout.BeginHorizontal(); GUILayout.Space(18f); if (GUILayout.Button(string.Empty, addButtonStyle)) { Undo.RegisterUndo(specifics, "add array item"); List <T> list = new List <T>(array as IEnumerable <T>); list.Add(construct()); array = list.ToArray(); EditorUtility.SetDirty(specifics); } if (array.Length == 0 && addAll != null) { GUILayout.Space(5f); if (GUILayout.Button("Add Default Entries", GUILayout.Width(120f))) { Undo.RegisterUndo(specifics, "add default entries"); T[] newArray = addAll(); array = (newArray == null) ? array : newArray; //Only assign if new array isn't null } } GUILayout.EndHorizontal(); }