public void SetPolymorphicElementDrawCallback(StratusSerializedField serializedProperty)
        {
            this.drawElementCallback =
                (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                if (!serializedProperty.isExpanded)
                {
                    return;
                }

                // Get the drawer for the element type
                object element     = serializedProperty.GetArrayElementAtIndex(index);
                Type   elementType = element.GetType();
                StratusSerializedEditorObject.ObjectDrawer drawer = StratusSerializedEditorObject.GetObjectDrawer(elementType);

                // Draw the element
                Rect position = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
                if (this.drawElementTypeLabel)
                {
                    EditorGUI.LabelField(position, elementType.Name, elementLabelStyle);
                    position.y += StratusEditorUtility.lineHeight;
                }
                drawer.DrawEditorGUI(position, element);
            };
        }
 public void SetPolymorphic(StratusSerializedField serializedField)
 {
     this.SetHeaderCallback(serializedField);
     this.SetPolymorphicElementDrawCallback(serializedField);
     this.SetPolymorphicElementHeightCallback(serializedField);
     this.SetElementAddCallback(serializedField);
 }
        /// <summary>
        /// Draws a property (really, a field) serialized by Odin Serializer
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public bool DrawSerializedProperty(StratusSerializedField property)
        {
            bool changed = false;

            EditorGUI.BeginChangeCheck();
            {
                if (property.isList)
                {
                    //Debug.Log($"Now drawing the list { property.displayName}");
                    this.reorderableLists[this.customSerializedPropertyModels[property]].DoLayoutList();
                }
                else
                {
                    changed |= StratusEditorGUILayout.Field(property.field, this.target);
                }
            }
            if (EditorGUI.EndChangeCheck())
            {
                //StratusDebug.Log($"Detected change on {property.displayName}");
                EditorUtility.SetDirty(this.target);
                // Record change
                Undo.RecordObject(this.target, property.field.Name);
                // Apply the modified property
                this.serializedObject.ApplyModifiedProperties();
                changed = true;
            }
            return(changed);
        }
 public void SetHeaderCallback(StratusSerializedField serializedProperty)
 {
     this.drawHeaderCallback = (Rect rect) =>
     {
         Rect 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}) ");
     };
 }
Esempio n. 5
0
        //------------------------------------------------------------------------/
        // Procedures
        //------------------------------------------------------------------------/
        private static StratusSerializedField[] GetChildFields(StratusSerializedField parent)
        {
            List <StratusSerializedField> children = new List <StratusSerializedField>();

            foreach (var childField in parent.field.FieldType.GetSerializedFields())
            {
                children.Add(new StratusSerializedField(childField, parent.value));
            }
            return(children.ToArray());
        }
        public static StratusReorderableList PolymorphicList(StratusSerializedField serializedProperty)
        {
            if (!serializedProperty.isList)
            {
                throw new ArgumentException($"The field {serializedProperty.displayName} is not an array!");
            }

            IList list            = serializedProperty.asList;
            Type  baseElementType = Utilities.StratusReflection.GetIndexedType(list);
            StratusReorderableList reorderableList = new StratusReorderableList(list, baseElementType, true, true, true, true);

            reorderableList.SetPolymorphic(serializedProperty);
            return(reorderableList);
        }
        //------------------------------------------------------------------------/
        // Procedures
        //------------------------------------------------------------------------/
        private void GenerateFields()
        {
            FieldInfo[] fields = this.type.GetSerializedFields();
            List <StratusSerializedField> serializedFields = new List <StratusSerializedField>();

            // Backwards since we want the top-most declared classes first
            foreach (FieldInfo field in fields.Reverse())
            {
                StratusSerializedField property = new StratusSerializedField(field, this.target);
                this.fieldsByname.Add(property.name, property);
                serializedFields.Add(property);
            }

            this.fields = serializedFields.ToArray();
        }
        public void SetElementAddCallback(StratusSerializedField serializedProperty)
        {
            this.onAddDropdownCallback = (Rect buttonRect, ReorderableList list) =>
            {
                Type        baseType  = serializedProperty.listElementType;
                GenericMenu menu      = new GenericMenu();
                string[]    typeNames = Utilities.StratusReflection.GetSubclassNames(baseType);
                menu.AddItems(typeNames, (int index) =>
                {
                    serializedProperty.asList.Add(Utilities.StratusReflection.Instantiate(Utilities.StratusReflection.GetSubclass(baseType)[index]));
                });
                menu.ShowAsContext();
            };

            this.displayAdd = true;
        }
Esempio n. 9
0
            public Query(SerializedObject serializedObject, Type declaringType)
            {
                FieldInfo[] fields = declaringType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);

                List <StratusSerializedPropertyModel> propertyModels       = new List <StratusSerializedPropertyModel>();
                List <SerializedProperty>             serializedProperties = new List <SerializedProperty>();
                List <StratusSerializedField>         serializedFields     = new List <StratusSerializedField>();

                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);


                        //Debug.LogWarning($"{field.Name} is serialized by odin?{serializedbyOdin} unity?{serializedByUnity}");


                        // Odin
                        if (serializedbyOdin && !serializedByUnity)
                        {
                            StratusSerializedField serializedField = new StratusSerializedField(field, serializedObject.targetObject);
                            serializedFields.Add(serializedField);
                            propertyModels.Add(new StratusSerializedPropertyModel(serializedField));
                        }
                        // Unity
                        else
                        {
                            SerializedProperty property = serializedObject.FindProperty(field.Name);
                            if (property != null)
                            {
                                serializedProperties.Add(property);
                                propertyModels.Add(new StratusSerializedPropertyModel(property, field));
                            }
                        }
                    }
                }

                this.models           = propertyModels.ToArray();
                this.unitySerialized  = serializedProperties.ToArray();
                this.customSerialized = serializedFields.ToArray();
            }
        public void SetPolymorphicElementHeightCallback(StratusSerializedField serializedProperty)
        {
            this.elementHeightCallback = (int indexer) =>
            {
                if (!serializedProperty.isExpanded)
                {
                    return(0);
                }
                else
                {
                    StratusSerializedEditorObject.ObjectDrawer drawer = StratusSerializedEditorObject.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 (this.drawElementTypeLabel)
                    {
                        height += StratusSerializedEditorObject.DefaultObjectDrawer.lineHeight;
                    }

                    return(height);
                }
            };
        }
        //------------------------------------------------------------------------/
        // Utility
        //------------------------------------------------------------------------/
        /// <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[], StratusSerializedField[]> GetSerializedProperties(SerializedObject serializedObject, Type type)
        {
            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);
            //FieldInfo[] fields = type.GetSerializedFields(true);

            List <SerializedProperty>     serializedProperties     = new List <SerializedProperty>();
            List <StratusSerializedField> odinSerializedProperties = new List <StratusSerializedField>();

            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)
                    {
                        StratusSerializedField property = new StratusSerializedField(field, serializedObject.targetObject);
                        odinSerializedProperties.Add(property);
                    }
                    // Unity
                    else
                    {
                        SerializedProperty property = serializedObject.FindProperty(field.Name);
                        if (property != null)
                        {
                            serializedProperties.Add(property);
                        }
                    }
                }
            }

            return(new Tuple <SerializedProperty[], StratusSerializedField[]>(serializedProperties.ToArray(), odinSerializedProperties.ToArray()));
        }
        public static StratusReorderableList List(FieldInfo field, object target)
        {
            StratusSerializedField odinSerializedProperty = new StratusSerializedField(field, target);

            return(PolymorphicList(odinSerializedProperty));
        }