Exemplo n.º 1
0
        /// <summary>
        /// Draws main editor
        /// </summary>
        protected void DrawCustomEditor()
        {
            EditorGUILayout.BeginVertical();

            Type targetType = target.GetType();
            // Get all the members of this type, public and private
            List <MemberInfo> members = new List <MemberInfo>(targetType.GetMembers(defaultBindingFlags));

            members.Sort(
                delegate(MemberInfo m1, MemberInfo m2)
            {
                if (m1.IsDefined(typeof(DrawLastAttribute), true))
                {
                    return(1);
                }
                return(0);
            }
                );

            // Start drawing the editor
            int  currentIndentLevel = 0;
            bool insideSectionBlock = false;
            bool drawCurrentSection = true;

            foreach (MemberInfo member in members)
            {
                try
                {
                    // First get header and indent settings
                    if (member.IsDefined(typeof(HeaderAttribute), true))
                    {
                        HeaderAttribute header = member.GetCustomAttributes(typeof(HeaderAttribute), true)[0] as HeaderAttribute;
                        if (insideSectionBlock)
                        {
                            DrawSectionEnd();
                        }

                        insideSectionBlock = true;
                        drawCurrentSection = DrawSectionStart(target.GetType().Name, header.header);
                    }

                    // Then do basic show / hide based on ShowIfAttribute
                    if ((insideSectionBlock && !drawCurrentSection) || !ShouldDrawMember(member, targetType, target))
                    {
                        continue;
                    }

                    // Handle drawing stuff (indent, help)
                    if (showHelp)
                    {
                        DrawToolTip(member);
                    }

                    if (member.IsDefined(typeof(SetIndentAttribute), true))
                    {
                        SetIndentAttribute indent = member.GetCustomAttributes(typeof(SetIndentAttribute), true)[0] as SetIndentAttribute;
                        currentIndentLevel   += indent.Indent;
                        EditorGUI.indentLevel = currentIndentLevel;
                    }

                    // Now get down to drawing the thing
                    // Get an array ready for our override attributes
                    object[] drawOverrideAttributes = null;
                    switch (member.MemberType)
                    {
                    case MemberTypes.Field:
                        FieldInfo field = targetType.GetField(member.Name, defaultBindingFlags);
                        if (!field.IsPrivate || field.IsDefined(typeof(SerializeField), true))
                        {
                            // If it's a profile field, take care of that first
                            if (IsSubclassOf(field.FieldType, typeof(ProfileBase)))
                            {
                                UnityEngine.Object profile = (UnityEngine.Object)field.GetValue(target);
                                profile = DrawProfileField(target, profile, field.FieldType);
                                field.SetValue(target, profile);
                            }
                            else
                            {
                                drawOverrideAttributes = field.GetCustomAttributes(typeof(DrawOverrideAttribute), true);
                                // If we fine overrides, draw using those
                                if (drawOverrideAttributes.Length > 0)
                                {
                                    if (drawOverrideAttributes.Length > 1)
                                    {
                                        DrawWarning("You should only use one DrawOverride attribute per member. Drawing " + drawOverrideAttributes[0].GetType().Name + " only.");
                                    }

                                    (drawOverrideAttributes[0] as DrawOverrideAttribute).DrawEditor(target, field, serializedObject.FindProperty(field.Name));
                                }
                                else
                                {
                                    // Otherwise just draw the default editor
                                    DrawSerializedField(serializedObject, field.Name);
                                }
                            }
                        }
                        break;

                    case MemberTypes.Property:
                        // We have to draw properties manually
                        PropertyInfo prop = targetType.GetProperty(member.Name, defaultBindingFlags);
                        drawOverrideAttributes = prop.GetCustomAttributes(typeof(DrawOverrideAttribute), true);
                        // If it's a profile field, take care of that first
                        if (IsSubclassOf(prop.PropertyType, typeof(ProfileBase)))
                        {
                            UnityEngine.Object profile = (UnityEngine.Object)prop.GetValue(target, null);
                            profile = DrawProfileField(target, profile, prop.PropertyType);
                            prop.SetValue(target, profile, null);
                        }
                        // If we find overrides, draw using those
                        else if (drawOverrideAttributes.Length > 0)
                        {
                            if (drawOverrideAttributes.Length > 1)
                            {
                                DrawWarning("You should only use one DrawOverride attribute per member. Drawing " + drawOverrideAttributes[0].GetType().Name + " only.");
                            }

                            (drawOverrideAttributes[0] as DrawOverrideAttribute).DrawEditor(target, prop);
                        }
                        break;

                    default:
                        // Don't do anything, it's not something we can use
                        break;
                    }
                }
                catch (Exception e)
                {
                    DrawWarning("There was a problem drawing the member " + member.Name + ":");
                    DrawError(System.Environment.NewLine + e.ToString());
                }
            }

            if (insideSectionBlock)
            {
                DrawSectionEnd();
            }

            EditorGUILayout.EndVertical();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Draws main editor
        /// </summary>
        protected void DrawCustomEditor()
        {
            EditorGUILayout.BeginVertical();

            Type targetType = target.GetType();
            // Get all the members of this type, public and private
            List <MemberInfo> members = new List <MemberInfo>(targetType.GetMembers(defaultBindingFlags));

            // Start drawing the editor
            int  currentIndentLevel = 0;
            bool insideSectionBlock = false;
            bool drawCurrentSection = true;

            foreach (MemberInfo member in members)
            {
                try
                {
                    switch (member.MemberType)
                    {
                    default:
                        // We only want to draw fields, properties and arrays
                        continue;

                    case MemberTypes.Field:
                    case MemberTypes.Property:
                        break;
                    }

                    // First get header and indent settings
                    if (member.IsDefined(typeof(HeaderAttribute), true))
                    {
                        HeaderAttribute header = member.GetCustomAttributes(typeof(HeaderAttribute), true)[0] as HeaderAttribute;
                        if (insideSectionBlock)
                        {
                            DrawSectionEnd();
                        }

                        insideSectionBlock = true;
                        drawCurrentSection = DrawSectionStart(target.GetType().Name, header.header);
                    }

                    // Then do basic show / hide based on ShowIfAttribute
                    if ((insideSectionBlock && !drawCurrentSection) || !ShouldDrawMember(member, targetType, target))
                    {
                        continue;
                    }

                    // Handle drawing stuff (indent, help)
                    if (showHelp)
                    {
                        DrawToolTip(member);
                    }

                    if (member.IsDefined(typeof(SetIndentAttribute), true))
                    {
                        SetIndentAttribute indent = member.GetCustomAttributes(typeof(SetIndentAttribute), true)[0] as SetIndentAttribute;
                        currentIndentLevel   += indent.Indent;
                        EditorGUI.indentLevel = currentIndentLevel;
                    }

                    if (member.IsDefined(typeof(FeatureInProgressAttribute), true))
                    {
                        GUI.color = helpBoxColor;
                        EditorGUILayout.LabelField("(This field or property is marked as 'In Progress' and may have no effect.)", inProgressStyle);
                        GUI.color = Color.Lerp(disabledColor, Color.clear, 0.5f);
                    }
                    else
                    {
                        GUI.color = defaultColor;
                    }

                    // Now get down to drawing the thing
                    // Get an array ready for our override attributes
                    object[] drawOverrideAttributes = null;
                    if (member.MemberType == MemberTypes.Field)
                    {
                        FieldInfo field = targetType.GetField(member.Name, defaultBindingFlags);
                        if (!field.IsPrivate || field.IsDefined(typeof(SerializeField), true))
                        {
                            // If it's a profile field, take care of that first
                            if (IsSubclassOf(field.FieldType, typeof(ProfileBase)))
                            {
                                UnityEngine.Object profile = (UnityEngine.Object)field.GetValue(target);
                                profile = DrawProfileField(target, profile, field.FieldType);
                                field.SetValue(target, profile);
                            }
                            else
                            {
                                drawOverrideAttributes = field.GetCustomAttributes(typeof(DrawOverrideAttribute), true);
                                // If we fine overrides, draw using those
                                if (drawOverrideAttributes.Length > 0)
                                {
                                    if (drawOverrideAttributes.Length > 1)
                                    {
                                        DrawWarning("You should only use one DrawOverride attribute per member. Drawing " + drawOverrideAttributes[0].GetType().Name + " only.");
                                    }

                                    (drawOverrideAttributes[0] as DrawOverrideAttribute).DrawEditor(target, field, serializedObject.FindProperty(field.Name));
                                }
                                else
                                {
                                    // Otherwise just draw the default editor
                                    DrawSerializedField(serializedObject, field.Name);
                                }
                            }
                        }
                    }
                    else // Property
                    {
                        // We have to draw properties manually
                        PropertyInfo prop = targetType.GetProperty(member.Name, defaultBindingFlags);
                        drawOverrideAttributes = prop.GetCustomAttributes(typeof(DrawOverrideAttribute), true);
                        // If it's a profile field, take care of that first
                        if (IsSubclassOf(prop.PropertyType, typeof(ProfileBase)))
                        {
                            UnityEngine.Object profile = (UnityEngine.Object)prop.GetValue(target, null);
                            profile = DrawProfileField(target, profile, prop.PropertyType);
                            prop.SetValue(target, profile, null);
                        }
                        // If we find overrides, draw using those
                        else if (drawOverrideAttributes.Length > 0)
                        {
                            if (drawOverrideAttributes.Length > 1)
                            {
                                DrawWarning("You should only use one DrawOverride attribute per member. Drawing " + drawOverrideAttributes[0].GetType().Name + " only.");
                            }

                            (drawOverrideAttributes[0] as DrawOverrideAttribute).DrawEditor(target, prop);
                        }
                    }
                }
                catch (Exception e)
                {
                    DrawWarning("There was a problem drawing the member " + member.Name + ":");
                    DrawError(Environment.NewLine + e.ToString());
                }
            }

            if (insideSectionBlock)
            {
                DrawSectionEnd();
            }

            EditorGUILayout.EndVertical();
        }