示例#1
0
        public static Drawer CreateAttrDrawer(DrawerProperty property, DrawerAttribute attr)
        {
            if (attrDrawerDic.TryGetValue(attr.GetType(), out Type drawerType))
            {
                Drawer drawer = (Drawer)Activator.CreateInstance(drawerType);
                drawer.Attr     = attr;
                drawer.Property = property;

                return(drawer);
            }

            return(null);
        }
示例#2
0
        private void DrawField()
        {
            var isPropertyEnabled = this.IsEnabled();

            // Draw the field
            EditorGUI.BeginChangeCheck();
            GUI.enabled = isPropertyEnabled;

            PropertyDrawer  drawer    = null;
            DrawerAttribute attribute = null;

            var drawerAttributes = this.GetCustomAttributes <DrawerAttribute>().ToArray();

            if (drawerAttributes.Length > 0)
            {
                attribute = drawerAttributes[0];
                drawer    = PropertyDrawerDatabase.GetDrawerForAttribute(attribute.GetType());
            }

            if (this.HasChildren)
            {
                this.foldout = EditorGUILayout.Foldout(this.foldout, this.DisplayName);

                if (this.foldout)
                {
                    EditorGUI.indentLevel += 1;
                    this.DrawPropertyField(drawer, attribute);
                    EditorGUI.indentLevel -= 1;
                }
            }
            else if (this.IsArray)
            {
                this.foldout = EditorGUILayout.Foldout(this.foldout, this.DisplayName);

                if (this.foldout)
                {
                    this.DrawPropertyField(drawer, attribute);
                }
            }
            else
            {
                this.DrawPropertyField(drawer, attribute);
            }

            GUI.enabled = true;

            if (EditorGUI.EndChangeCheck())
            {
                this.OnEndChangeCheck();
            }
        }
示例#3
0
        public static IAttrDrawer GetAttrDrawerInstance(DrawerAttribute drawerAttr)
        {
            if (attrTypeDrawerDic == null)
            {
                LoadAttrDrawers();
            }

            if (attrTypeDrawerDic.TryGetValue(drawerAttr.GetType(), out var drawerType) && drawerType != null)
            {
                IAttrDrawer drawer = (IAttrDrawer)Activator.CreateInstance(drawerType);
                drawer.Attr = drawerAttr;
                return(drawer);
            }
            return(null);
        }
 /// <inheritdoc />
 protected override void DrawPropertyField(PropertyDrawer drawer, DrawerAttribute attribute)
 {
     if (this.HasChildren)
     {
         this.childDrawer.OnInspectorGUI();
     }
     else if (this.IsArray)
     {
         ListPropertyDrawer.Instance.DrawArray(this.Property);
     }
     else
     {
         if (drawer == null || attribute == null)
         {
             this.DrawDefaultField();
         }
         else
         {
             drawer.Run(this, attribute);
         }
     }
 }
示例#5
0
        /// <inheritdoc />
        protected override void DrawPropertyField(PropertyDrawer drawer, DrawerAttribute attribute)
        {
            if (this.isArray)
            {
                return;
            }

            if (this.HasChildren)
            {
                this.childDrawer.OnInspectorGUI();
            }
            else
            {
                if (drawer == null || attribute == null)
                {
                    this.DrawDefaultField();
                }
                else
                {
                    drawer.Run(this, attribute);
                }
            }
        }
示例#6
0
        public override void OnInspectorGUI()
        {
            if (this.useDefaultInspector)
            {
                this.DrawDefaultInspector();
            }
            else
            {
                this.serializedObject.Update();

                if (this.script != null)
                {
                    GUI.enabled = false;
                    EditorGUILayout.PropertyField(this.script);
                    GUI.enabled = true;
                }

                // Draw fields
                HashSet <string> drawnGroups = new HashSet <string>();
                foreach (var field in this.fields)
                {
                    if (this.groupedFields.Contains(field))
                    {
                        // Draw grouped fields
                        string groupName = (field.GetCustomAttributes(typeof(GroupAttribute), true)[0] as GroupAttribute).Name;
                        if (!drawnGroups.Contains(groupName))
                        {
                            drawnGroups.Add(groupName);

                            PropertyGrouper grouper = this.GetPropertyGrouperForField(field);
                            if (grouper != null)
                            {
                                grouper.BeginGroup(groupName);

                                this.ValidateAndDrawFields(this.groupedFieldsByGroupName[groupName]);

                                grouper.EndGroup();
                            }
                            else
                            {
                                this.ValidateAndDrawFields(this.groupedFieldsByGroupName[groupName]);
                            }
                        }
                    }
                    else
                    {
                        // Draw non-grouped field
                        this.ValidateAndDrawField(field);
                    }
                }

                this.serializedObject.ApplyModifiedProperties();
            }

            // Draw non-serialized fields
            foreach (var field in this.nonSerializedFields)
            {
                DrawerAttribute drawerAttribute = (DrawerAttribute)field.GetCustomAttributes(typeof(DrawerAttribute), true)[0];
                FieldDrawer     drawer          = FieldDrawerDatabase.GetDrawerForAttribute(drawerAttribute.GetType());
                if (drawer != null)
                {
                    drawer.DrawField(this.target, field);
                }
            }

            // Draw native properties
            foreach (var property in this.nativeProperties)
            {
                DrawerAttribute      drawerAttribute = (DrawerAttribute)property.GetCustomAttributes(typeof(DrawerAttribute), true)[0];
                NativePropertyDrawer drawer          = NativePropertyDrawerDatabase.GetDrawerForAttribute(drawerAttribute.GetType());
                if (drawer != null)
                {
                    drawer.DrawNativeProperty(this.target, property);
                }
            }

            // Draw methods
            foreach (var method in this.methods)
            {
                DrawerAttribute drawerAttribute = (DrawerAttribute)method.GetCustomAttributes(typeof(DrawerAttribute), true)[0];
                MethodDrawer    methodDrawer    = MethodDrawerDatabase.GetDrawerForAttribute(drawerAttribute.GetType());
                if (methodDrawer != null)
                {
                    methodDrawer.DrawMethod(this.target, method);
                }
            }
        }
        public override void OnInspectorGUI()
        {
            this.serializedObject.Update();

            if (this.script != null)
            {
                GUI.enabled = false;
                EditorGUILayout.PropertyField(this.script);
                GUI.enabled = true;
            }

            // Draw fields
            IEnumerable <FieldInfo> fields =
                this.target.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
                .Where(f => this.serializedObject.FindProperty(f.Name) != null);

            IEnumerable <FieldInfo> groupedFields = fields.Where(f => f.GetCustomAttributes(typeof(GroupAttribute), true).Length > 0);

            HashSet <string> groupNames = new HashSet <string>();

            foreach (var field in fields)
            {
                GroupAttribute[] groupAttributes = (GroupAttribute[])field.GetCustomAttributes(typeof(GroupAttribute), true);
                if (groupAttributes.Length > 0)
                {
                    // Draw grouped fields
                    string groupName = groupAttributes[0].Name;
                    if (!groupNames.Contains(groupName))
                    {
                        groupNames.Add(groupName);

                        IEnumerable <FieldInfo> fieldsInSameGroup = groupedFields
                                                                    .Where(f => (f.GetCustomAttributes(typeof(GroupAttribute), true) as GroupAttribute[])[0].Name == groupName);

                        PropertyGrouper grouper = this.GetGrouperForField(field);
                        if (grouper != null)
                        {
                            grouper.BeginGroup(groupName);

                            this.ValidateAndDrawFields(fieldsInSameGroup);

                            grouper.EndGroup();
                        }
                        else
                        {
                            this.ValidateAndDrawFields(fieldsInSameGroup);
                        }
                    }
                }
                else
                {
                    // Draw non-grouped field
                    this.ValidateAndDrawField(field);
                }
            }

            // Draw methods
            IEnumerable <MethodInfo> methods =
                this.target.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public)
                .Where(m => m.GetCustomAttributes(typeof(DrawerAttribute), true).Length > 0);

            foreach (var method in methods)
            {
                DrawerAttribute drawerAttribute = (DrawerAttribute)method.GetCustomAttributes(typeof(DrawerAttribute), true)[0];
                MethodDrawer    methodDrawer    = MethodDrawerDatabase.GetDrawerForAttribute(drawerAttribute.GetType());
                if (methodDrawer != null)
                {
                    methodDrawer.DrawMethod(this.serializedObject.targetObject, method);
                }
            }

            this.serializedObject.ApplyModifiedProperties();
        }
示例#8
0
 protected abstract void DrawPropertyField(PropertyDrawer drawer, DrawerAttribute attribute);