public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        MultiPropertyAttribute mAttribute = RetrieveAttributes();

        // Calls to IsVisible. If it returns false for any attribute, the property will not be rendered.
        foreach (MultiPropertyAttribute attr in mAttribute.stored)
        {
            if (!attr.IsVisible(property))
            {
                return;
            }
        }

        // Calls to OnPreRender before the last attribute draws the UI.
        foreach (MultiPropertyAttribute attr in mAttribute.stored)
        {
            attr.OnPreGUI(position, property);
        }

        // The last attribute is in charge of actually drawing something:
        ((MultiPropertyAttribute)mAttribute.stored.Last()).OnGUI(position, property, label);

        // Calls to OnPostRender after the last attribute draws the UI. These are called in reverse order.
        foreach (MultiPropertyAttribute attr in mAttribute.stored.Reverse())
        {
            attr.OnPostGUI(position, property);
        }
    }
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        MultiPropertyAttribute mAttribute = RetrieveAttributes();

        // If the attribute is invisible, regain the standard vertical spacing.
        foreach (MultiPropertyAttribute attr in mAttribute.stored)
        {
            if (!attr.IsVisible(property))
            {
                return(-EditorGUIUtility.standardVerticalSpacing);
            }
        }

        // In case no attribute returns a modified height, return the property's default one:
        float height = base.GetPropertyHeight(property, label);

        // Check if any of the attributes wants to modify height:
        foreach (object atr in mAttribute.stored)
        {
            if (atr as MultiPropertyAttribute != null)
            {
                var tempheight = ((MultiPropertyAttribute)atr).GetPropertyHeight(property, label);
                if (tempheight.HasValue)
                {
                    height = tempheight.Value;
                    break;
                }
            }
        }
        return(height);
    }
    private MultiPropertyAttribute RetrieveAttributes()
    {
        MultiPropertyAttribute mAttribute = attribute as MultiPropertyAttribute;

        // Get the attribute list, sorted by "order".
        if (mAttribute.stored == null)
        {
            mAttribute.stored = fieldInfo.GetCustomAttributes(typeof(MultiPropertyAttribute), false).OrderBy(s => ((PropertyAttribute)s).order);
        }

        return(mAttribute);
    }
Пример #4
0
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            MultiPropertyAttribute thisAttribute = this.attribute as MultiPropertyAttribute;

            float height = base.GetPropertyHeight(property, label);

            if (thisAttribute != null && thisAttribute.stored.Count > 0)
            {
                height = thisAttribute.stored.Max(attr => attr.GetPropertyHeight(property, label) ?? height);
            }
            return(height);
        }
Пример #5
0
        // Draw the property inside the given rect
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            MultiPropertyAttribute thisAttribute = this.attribute as MultiPropertyAttribute;

            if (thisAttribute == null)
            {
                base.OnGUI(position, property, label);
                return;
            }

            // First get the attribute since it contains the range for the slider
            if (thisAttribute.stored == null || thisAttribute.stored.Count == 0)
            {
                thisAttribute.stored = this.fieldInfo.GetCustomAttributes(typeof(MultiPropertyAttribute), false)
                                       .Cast <MultiPropertyAttribute>()
                                       .OrderBy(a => a.order).ToList();
            }

            // Store current GUI state
            string origTooltip         = GUI.tooltip;
            Color  origBackgroundColor = GUI.backgroundColor;
            Color  origColor           = GUI.color;
            Color  origContentColor    = GUI.contentColor;
            bool   origEnabled         = GUI.enabled;
            int    origIndentLevel     = EditorGUI.indentLevel;

            bool propertyDrawn = false;

            foreach (var attr in thisAttribute.stored)
            {
                label          = attr.BuildLabel(label);
                propertyDrawn |= attr.OnGUI(position, property, label);
            }
            if (!propertyDrawn)
            {
                EditorGUI.PropertyField(position, property, label);
            }

            // Restore GUI state
            GUI.tooltip           = origTooltip;
            GUI.backgroundColor   = origBackgroundColor;
            GUI.color             = origColor;
            GUI.contentColor      = origContentColor;
            GUI.enabled           = origEnabled;
            EditorGUI.indentLevel = origIndentLevel;
        }