/// <summary> /// Draws a serialized property, saving any recorded changes /// </summary> /// <param name="property"></param> public static bool DrawSerializedProperty(SerializedProperty property, SerializedObject serializedObject) { EditorGUI.BeginChangeCheck(); // Arrays if (property.isArray && property.propertyType != SerializedPropertyType.String) { StratusReorderableList.List(property); } else { EditorGUILayout.PropertyField(property, true); } // If property was changed, save if (EditorGUI.EndChangeCheck()) { // Record change Undo.RecordObject(property.objectReferenceValue, property.name); serializedObject.ApplyModifiedProperties(); return(true); } return(false); }
public static StratusReorderableList List(SerializedProperty serializedProperty, Type baseElementType) { var reorderableList = new StratusReorderableList(serializedProperty.serializedObject, serializedProperty, true, true, true, true); reorderableList.SetDefault(serializedProperty); return(reorderableList); }
public static StratusReorderableList List(SerializedProperty serializedProperty, string title) { StratusReorderableList reorderableList = List(serializedProperty); reorderableList.title = title; return(reorderableList); }
public static StratusReorderableList List(SerializedProperty serializedProperty) { StratusReorderableList reorderableList = new StratusReorderableList(serializedProperty.serializedObject, serializedProperty, true, true, true, true); reorderableList.SetDefault(serializedProperty); return(reorderableList); }
public static void DrawCachedPolymorphicList(FieldInfo field, object target) { IList list = field.GetValue(target) as IList; if (!cachedLists.ContainsKey(list)) { StratusReorderableList reorderableList = PolymorphicList(field, target); cachedLists.Add(list, reorderableList); } cachedLists[list].DoLayoutList(); }
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 override bool DrawEditorGUILayout(object target, bool isChild = false) { EditorGUI.BeginChangeCheck(); switch (propertyType) { case SerializedPropertyType.ObjectReference: this.field.SetValue(target, UnityEditor.EditorGUILayout.ObjectField(name, this.GetValue <UnityEngine.Object>(target), type, true)); break; case SerializedPropertyType.Integer: this.field.SetValue(target, UnityEditor.EditorGUILayout.IntField(name, this.GetValue <int>(target))); break; case SerializedPropertyType.Boolean: this.field.SetValue(target, UnityEditor.EditorGUILayout.Toggle(name, this.GetValue <bool>(target))); break; case SerializedPropertyType.Float: OnFloatEditorGUILayout(target); break; case SerializedPropertyType.String: this.field.SetValue(target, UnityEditor.EditorGUILayout.TextField(name, this.GetValue <string>(target))); break; case SerializedPropertyType.Color: this.field.SetValue(target, UnityEditor.EditorGUILayout.ColorField(name, this.GetValue <Color>(target))); break; case SerializedPropertyType.LayerMask: this.field.SetValue(target, UnityEditor.EditorGUILayout.LayerField(name, this.GetValue <LayerMask>(target))); break; case SerializedPropertyType.Enum: this.field.SetValue(target, UnityEditor.EditorGUILayout.EnumPopup(name, this.GetValue <Enum>(target))); break; case SerializedPropertyType.Vector2: this.field.SetValue(target, UnityEditor.EditorGUILayout.Vector2Field(name, this.GetValue <Vector2>(target))); break; case SerializedPropertyType.Vector3: this.field.SetValue(target, UnityEditor.EditorGUILayout.Vector3Field(name, this.GetValue <Vector3>(target))); break; case SerializedPropertyType.Vector4: this.field.SetValue(target, UnityEditor.EditorGUILayout.Vector4Field(name, this.GetValue <Vector4>(target))); break; case SerializedPropertyType.Rect: this.field.SetValue(target, UnityEditor.EditorGUILayout.RectField(name, this.GetValue <Rect>(target))); break; default: if (isArray) { EditorGUILayout.Space(); StratusReorderableList.DrawCachedPolymorphicList(this.field, target); } else { UnityEditor.EditorGUILayout.LabelField($"No drawer implementation for {name} of type {type.Name}"); } break; } if (EditorGUI.EndChangeCheck()) { return(true); } return(false); }
/// <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)); }
/// <summary> /// Adds all SerializedProperties to be inspected /// </summary> internal void ScanProperties() { // Reset the number of drawn properties this.drawnProperties = 0; // For every type, starting from the most derived up to the base, get its serialized properties Type currentType = this.target.GetType(); Type previousType = null; while (currentType != this.baseType && currentType != null) { // Serialized Properties StratusSerializedPropertyModel.Query query = new StratusSerializedPropertyModel.Query(serializedObject, currentType); foreach (StratusSerializedPropertyModel propertyModel in query.models) { if (propertyModel == null) { Debug.LogError($"A property was found to not be serialized properly while inspecting {this.target.name}. Did you forget a [Serializable] attribute on a class definition?"); continue; } //Debug.Log($"Drawer for serialized property: {propertyModel.displayName} with type {propertyModel.type}"); switch (propertyModel.type) { case StratusSerializedPropertyModel.SerializationType.Unity: { SerializedProperty serializedProperty = propertyModel.unitySerialized; this.propertyMap.Add(serializedProperty.name, serializedProperty); //Debug.Log($"Added property {serializedProperty.name}"); this.unitySerializedPropertyModels.Add(serializedProperty, propertyModel); // Record the attributes for this property Attribute[] attributes = propertyModel.attributes; this.propertyAttributes.Add(serializedProperty, attributes); this.propertyAttributesMap.AddUnique(serializedProperty, new Dictionary <Type, Attribute>()); foreach (Attribute attr in attributes) { this.propertyAttributesMap[serializedProperty].AddUnique(attr.GetType(), attr); } this.OnPropertyAttributesAdded(serializedProperty); // Check whether this property is an array if (serializedProperty.isArray && serializedProperty.propertyType != SerializedPropertyType.String) { this.reorderableLists.Add(propertyModel, StratusReorderableList.List(serializedProperty)); //this.AddReorderableList(serializedProperty); } } break; case StratusSerializedPropertyModel.SerializationType.Custom: { this.customSerializedPropertyModels.Add(propertyModel.customSerialized, propertyModel); if (propertyModel.customSerialized.isList) { this.reorderableLists.Add(propertyModel, StratusReorderableList.PolymorphicList(propertyModel.customSerialized)); } } break; } } this.propertiesByType.Add(currentType, query.models); this.unityPropertiesByType.Add(currentType, query.unitySerialized); this.propertyGroups.Add(new Tuple <Type, StratusSerializedPropertyModel[]>(currentType, query.models)); //// Add all the properties for this type into the property map by type //if (!currentType.IsGenericType) //{ // this.propertiesByType.Add(currentType, query.models); // this.unityPropertiesByType.Add(currentType, query.unitySerialized); // this.propertyGroups.Add(new Tuple<Type, StratusSerializedPropertyModel[]>(currentType, query.models)); //} //else //{ // if (!previousType.IsGenericType) // { // SerializedProperty[] joinedUnityProperties = this.unityPropertiesByType[previousType].Concat(query.unitySerialized); // this.unityPropertiesByType[previousType] = joinedUnityProperties; // // Combined // StratusSerializedPropertyModel[] joinedProperties = this.propertiesByType[previousType].Concat(query.models); // this.propertiesByType[previousType] = joinedProperties; // // Concat property groups // this.propertyGroups.RemoveLast(); // this.propertyGroups.Add(new Tuple<Type, StratusSerializedPropertyModel[]>(previousType, joinedProperties)); // } //} // Move on to the parent type (if any) previousType = currentType; currentType = currentType.BaseType; } this.propertyGroups.Reverse(); }