/// <summary> /// Draws a property (really, a field) serialized by Odin Serializer /// </summary> /// <param name="property"></param> /// <returns></returns> public bool DrawSerializedProperty(StratusOdinSerializedProperty property) { bool changed = false; EditorGUI.BeginChangeCheck(); { if (property.isArray) { this.DrawReorderableList(property); } else { changed |= StratusEditorUtility.DrawField(property.field, this.target); } } if (EditorGUI.EndChangeCheck()) { EditorUtility.SetDirty(target); // Record change Undo.RecordObject(target, property.field.Name); // Apply the modified property serializedObject.ApplyModifiedProperties(); changed = true; } return(changed); }
public void SetPolymorphicElementDrawCallback(StratusOdinSerializedProperty serializedProperty) { this.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { if (!serializedProperty.isExpanded) { //EditorGUI.LabelField(rect, elementType.Name, elementLabelStyle); //GUI.enabled = index == count; return; } // Get the drawer for the element type var element = serializedProperty.GetArrayElementAtIndex(index); Type elementType = element.GetType(); SerializedSystemObject.ObjectDrawer drawer = SerializedSystemObject.GetObjectDrawer(elementType); // Draw the element Rect position = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight); if (drawElementTypeLabel) { EditorGUI.LabelField(position, elementType.Name, elementLabelStyle); position.y += StratusEditorUtility.lineHeight; } drawer.DrawEditorGUI(position, element); }; }
public void SetPolymorphic(StratusOdinSerializedProperty serializedProperty) { this.SetHeaderCallback(serializedProperty); this.SetPolymorphicElementDrawCallback(serializedProperty); this.SetPolymorphicElementHeightCallback(serializedProperty); this.SetElementAddCallback(serializedProperty); }
//------------------------------------------------------------------------/ // Helpers //------------------------------------------------------------------------/ /// <summary> /// Gets all the serialized property for the given Unity Object of a specified type /// </summary> /// <param name="obj"></param> /// <param name="type"></param> /// <returns></returns> public static Tuple <SerializedProperty[], StratusOdinSerializedProperty[]> GetSerializedProperties(SerializedObject serializedObject, Type type) { FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); List <SerializedProperty> serializedProperties = new List <SerializedProperty>(); List <StratusOdinSerializedProperty> odinSerializedProperties = new List <StratusOdinSerializedProperty>(); for (int i = 0; i < fields.Length; i++) { FieldInfo field = fields[i]; if (field != null && (field.Attributes != FieldAttributes.NotSerialized)) { bool serializedbyOdin = OdinSerializer.UnitySerializationUtility.OdinWillSerialize(field, true); bool serializedByUnity = OdinSerializer.UnitySerializationUtility.GuessIfUnityWillSerialize(field); // Odin if (serializedbyOdin && !serializedByUnity) { StratusOdinSerializedProperty property = new StratusOdinSerializedProperty(field, serializedObject.targetObject); odinSerializedProperties.Add(property); } // Unity else { SerializedProperty property = serializedObject.FindProperty(field.Name); if (property != null) { serializedProperties.Add(property); } } } } return(new Tuple <SerializedProperty[], StratusOdinSerializedProperty[]>(serializedProperties.ToArray(), odinSerializedProperties.ToArray())); }
public void SetHeaderCallback(StratusOdinSerializedProperty serializedProperty) { this.drawHeaderCallback = (Rect rect) => { var newRect = new Rect(rect.x + 10, rect.y, rect.width - 10, rect.height); serializedProperty.isExpanded = EditorGUI.Foldout(newRect, serializedProperty.isExpanded, $"{serializedProperty.displayName} ({serializedProperty.listElementType.Name}) "); }; }
private void GenerateProperties() { FieldInfo[] fields = GetSerializedFields(this.type); List <StratusOdinSerializedProperty> properties = new List <StratusOdinSerializedProperty>(); foreach (var field in fields) { StratusOdinSerializedProperty property = new StratusOdinSerializedProperty(field, this.target); propertiesByName.Add(property.name, property); properties.Add(property); } this.properties = properties.ToArray(); }
public static StratusReorderableList PolymorphicList(StratusOdinSerializedProperty serializedProperty) { if (!serializedProperty.isArray) { throw new ArgumentException($"The field {serializedProperty.displayName} is not an array!"); } IList list = serializedProperty.list; Type baseElementType = Utilities.Reflection.GetIndexedType(list); var reorderableList = new StratusReorderableList(list, baseElementType, true, true, true, true); reorderableList.SetPolymorphic(serializedProperty); return(reorderableList); }
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; }
public void SetPolymorphicElementHeightCallback(StratusOdinSerializedProperty serializedProperty) { elementHeightCallback = (int indexer) => { if (!serializedProperty.isExpanded) { return(0); //return SerializedSystemObject.ObjectDrawer.lineHeight; } else { SerializedSystemObject.ObjectDrawer drawer = SerializedSystemObject.GetObjectDrawer(serializedProperty.GetArrayElementAtIndex(indexer)); float height = drawer.height; // We add an additional line of height since we are drawing a label for polymorphic list if (drawElementTypeLabel) { height += SerializedSystemObject.DefaultObjectDrawer.lineHeight; } return(height); } }; }
public static StratusReorderableList PolymorphicList(FieldInfo field, object target) { StratusOdinSerializedProperty odinSerializedProperty = new StratusOdinSerializedProperty(field, target); return(PolymorphicList(odinSerializedProperty)); }
/// <summary> /// Draws the given array type using a reorderable list drawer /// </summary> /// <param name="property"></param> /// <param name="label"></param> protected void DrawReorderableList(StratusOdinSerializedProperty property) { stratusReorderableLists[property].DoLayoutList(); }
/// <summary> /// Adds a reorderable list drawer to the editor /// </summary> /// <param name="property"></param> private void AddReorderableList(StratusOdinSerializedProperty property) { stratusReorderableLists.Add(property, StratusReorderableList.PolymorphicList(property)); }
public StratusSerializedPropertyModel(StratusOdinSerializedProperty serializedProperty) { this.odinSerialized = serializedProperty; this.type = SerializationType.Odin; }