Esempio n. 1
0
        public static void Field(Rect position, FieldInfo field, object target)
        {
            StratusSerializedFieldType propertyType = SerializedFieldTypeExtensions.Deduce(field);
            string name  = ObjectNames.NicifyVariableName(field.Name);
            object value = null;

            switch (propertyType)
            {
            case StratusSerializedFieldType.ObjectReference:
                value = EditorGUI.ObjectField(position, name, (UnityEngine.Object)field.GetValue(target), field.FieldType, true);
                break;

            case StratusSerializedFieldType.Integer:
                value = EditorGUI.IntField(position, field.GetValue <int>(target));
                break;

            case StratusSerializedFieldType.Boolean:
                value = EditorGUI.Toggle(position, name, field.GetValue <bool>(target));
                break;

            case StratusSerializedFieldType.Float:
                value = EditorGUI.FloatField(position, field.GetValue <float>(target));
                break;

            case StratusSerializedFieldType.String:
                value = EditorGUI.TextField(position, name, field.GetValue <string>(target));
                break;

            case StratusSerializedFieldType.Color:
                value = EditorGUI.ColorField(position, name, field.GetValue <Color>(target));
                break;

            case StratusSerializedFieldType.Enum:
                StratusSearchableEnum.EnumPopup(position, name, field.GetValue <Enum>(target), (Enum selected) => field.SetValue(target, selected));
                break;

            case StratusSerializedFieldType.Vector2:
                value = EditorGUI.Vector2Field(position, name, field.GetValue <Vector2>(target));
                break;

            case StratusSerializedFieldType.Vector3:
                value = EditorGUI.Vector3Field(position, name, field.GetValue <Vector3>(target));
                break;

            case StratusSerializedFieldType.Vector4:
                value = EditorGUI.Vector4Field(position, name, field.GetValue <Vector4>(target));
                break;

            case StratusSerializedFieldType.Rect:
                value = EditorGUI.RectField(position, name, field.GetValue <Rect>(target));
                break;

            default:
                EditorGUI.LabelField(position, $"No supported drawer for type {field.FieldType.Name}!");
                break;
            }
        }
            //------------------------------------------------------------------------/
            // CTOR
            //------------------------------------------------------------------------/
            public FieldDrawer(FieldInfo field)
            {
                this.field = field;
                this.type  = this.field.FieldType;
                this.name  = ObjectNames.NicifyVariableName(field.Name);

                this.attributesByName.AddRange((Attribute attribute) => attribute.GetType(), this.field.GetCustomAttributes(attributeType));
                this.propertyType = SerializedFieldTypeExtensions.Deduce(field);
                this.isPrimitive  = OdinSerializer.FormatterUtilities.IsPrimitiveType(this.type);
                this.isArray      = typeof(IList).IsAssignableFrom(this.type);
                // Drawable unless its meant to be hidden
                this.isDrawable = !this.attributesByName.ContainsKey(hideInInspectorAttribute);
                // All currently fields are eventually of a single line
                this.height = StratusEditorUtility.lineHeight;
            }
Esempio n. 3
0
        //------------------------------------------------------------------------/
        // CTOR
        //------------------------------------------------------------------------/
        public StratusSerializedField(FieldInfo field, object target)
        {
            this.field       = field;
            this.type        = this.field.FieldType;
            this.fieldType   = SerializedFieldTypeExtensions.Deduce(this.field);
            this.target      = target;
            this.isPrimitive = this.type.IsPrimitive;

            // Enum
            if (this.fieldType == StratusSerializedFieldType.Enum)
            {
                this.enumValueNames = Enum.GetNames(this.type);
            }

            // Array
            this.isList = typeof(IList).IsAssignableFrom(this.type);
            if (this.isList)
            {
                this.asList = this.field.GetValue(target) as IList;
                // Not yet instantiated
                if (this.asList == null)
                {
                    this.value  = Activator.CreateInstance(this.type);
                    this.asList = this.field.GetValue(target) as IList;
                }
                this.listElementType = Utilities.StratusReflection.GetIndexedType(this.asList);
            }
            else
            {
                if (!isPrimitive && this.fieldType != StratusSerializedFieldType.Enum)
                {
                    this.children       = GetChildFields(this);
                    this.childrenByName = this.children.ToDictionary <string, StratusSerializedField>((sf) => sf.name);
                }
            }

            // Get the display name
            this.displayName = field.GetNiceName();
        }
            protected DrawCommand[] GenerateDrawCommands(FieldInfo[] fields)
            {
                List <DrawCommand> drawers = new List <DrawCommand>();

                for (int i = 0; i < fields.Length; ++i)
                {
                    FieldInfo field     = fields[i];
                    Type      fieldType = field.FieldType;
                    StratusSerializedFieldType serializedPropertyType = SerializedFieldTypeExtensions.Deduce(field);

                    // Unity is supported by Unity if it's not a generic array
                    bool   isArray = IsArray(fieldType);
                    bool   isUnitySupportedType = (serializedPropertyType != StratusSerializedFieldType.Generic || isArray);                   //  OdinSerializer.FormatterUtilities.IsPrimitiveType(fieldType);
                    Drawer drawer = null;


                    if (isUnitySupportedType)
                    {
                        drawer = new FieldDrawer(field);
                    }
                    else
                    {
                        drawer = new DefaultObjectDrawer(field, fieldType);                         //  GetDrawer(field);
                        //drawer.displayName = field.Name;
                    }

                    //drawer.displayName = field.Name;
                    DrawCommand drawCommand = new DrawCommand(drawer, field, isUnitySupportedType);
                    if (drawer.isDrawable)
                    {
                        this.height += drawer.height;
                    }

                    drawers.Add(drawCommand);
                }

                return(drawers.ToArray());
            }