static public GenericMenu Create <T>(IEnumerable <T> options, Process <T> process) { GenericMenu menu = new GenericMenu(); menu.AddItems <T>(options, process); return(menu); }
public void SetElementAddCallback(StratusOdinSerializedProperty serializedProperty) { onAddDropdownCallback = (Rect buttonRect, ReorderableList list) => { Type baseType = serializedProperty.listElementType; var menu = new GenericMenu(); string[] typeNames = Utilities.Reflection.GetSubclassNames(baseType); menu.AddItems(typeNames, (int index) => { serializedProperty.list.Add(Utilities.Reflection.Instantiate(Utilities.Reflection.GetSubclass(baseType)[index])); }); menu.ShowAsContext(); }; this.displayAdd = true; }
/// <summary> /// Draws a list of elements deriving from a base class /// </summary> /// <returns>True if the height of the list changed, which signals a repaint event</returns> public static bool DrawPolymorphicList(FieldInfo field, object target, string title, bool useTypeLabel = true) { // We need to remember this list since the height is variable depending on the // amount of fields being drawn var fieldValue = field.GetValue(target); IList list = fieldValue as IList; //EditorGUILayout.BeginVertical(ReorderableListStyles.Container); // Draw the header EditorGUILayout.BeginHorizontal(ReorderableListStyles.Title); EditorGUILayout.PrefixLabel(title); if (GUILayout.Button("Add", polymorphicListLayoutOptions)) { Type baseType = Utilities.Reflection.GetIndexedType(list); // list.GetType().GetElementType(); var menu = new GenericMenu(); string[] typeNames = Utilities.Reflection.GetSubclassNames(baseType); menu.AddItems(typeNames, (int index) => { list.Add(Utilities.Reflection.Instantiate(Utilities.Reflection.GetSubclass(baseType)[index])); }); menu.ShowAsContext(); } // Clear if (GUILayout.Button("Clear", polymorphicListLayoutOptions)) { list.Clear(); } EditorGUILayout.EndHorizontal(); if (list.Count == 0) { return(false); } // Draw the elements bool changed = false; EditorGUILayout.BeginVertical(ReorderableListStyles.Container2); foreach (var element in list) { EditorGUILayout.Space(); // Get the drawer for the type Type elementType = element.GetType(); SerializedSystemObject.ObjectDrawer drawer = SerializedSystemObject.GetObjectDrawer(elementType); EditorGUILayout.BeginVertical(ReorderableListStyles.Container); { EditorGUILayout.LabelField(elementType.Name, EditorStyles.centeredGreyMiniLabel); //if (!drawer.isDrawable) // EditorGUILayout.LabelField($"There are no serialized fields for {elementType.Name}"); //else changed |= drawer.DrawEditorGUILayout(element); } EditorGUILayout.EndVertical(); } EditorGUILayout.Space(); EditorGUILayout.EndVertical(); //EditorGUILayout.EndVertical(); return(changed); }