Esempio n. 1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            HighlightAttribute highlightAttribute = (HighlightAttribute)attribute;

            bool doHighlight = true;

            if (!string.IsNullOrEmpty(highlightAttribute.validateMethod))
            {
                Type       t = property.serializedObject.targetObject.GetType();
                MethodInfo m = t.GetMethod(highlightAttribute.validateMethod, BindingFlags.Instance |
                                           BindingFlags.NonPublic | BindingFlags.Public);

                if (m != null)
                {
                    if (highlightAttribute.value != null)
                    {
                        doHighlight = (bool)m.Invoke(property.serializedObject.targetObject,
                                                     new object[] { highlightAttribute.value });
                    }
                    else
                    {
                        doHighlight = (bool)m.Invoke(property.serializedObject.targetObject, new object[] {});
                    }
                }
                else
                {
                    Debug.LogError("Invalid Validate function: " + highlightAttribute.validateMethod,
                                   property.serializedObject.targetObject);
                }
            }

            if (doHighlight)
            {
                Color colorHighlight = Colors.FromEnum(highlightAttribute.color);

                float padding           = EditorGUIUtility.standardVerticalSpacing;
                Rect  positionHighlight = new Rect(position.x - padding, position.y - padding,
                                                   position.width + (padding * 2), position.height + (padding * 2));

                EditorGUI.DrawRect(positionHighlight, colorHighlight);

                Color colorRestore = GUI.contentColor;

                GUI.contentColor = Color.black;
                EditorGUI.PropertyField(position, property, label);

                GUI.contentColor = colorRestore;
            }
            else
            {
                EditorGUI.PropertyField(position, property, label);
            }
        }
Esempio n. 2
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            StatBarAttribute statBar   = (StatBarAttribute)attribute;
            float            maxValue  = -1;
            string           typeError = "";

            if (statBar.valueMax != null)
            {
                if (statBar.valueMax is string)
                {
                    SerializedProperty propertyMax = property.serializedObject.FindProperty(
                        (string)statBar.valueMax);
                    if (propertyMax == null)
                    {
                        typeError = "valueMax refernaces an invalid property!";
                    }
                    else if (propertyMax.propertyType == SerializedPropertyType.Float)
                    {
                        maxValue = propertyMax.floatValue;
                        if (maxValue < 0)
                        {
                            typeError = "valueMax must be positive!";
                        }
                    }
                    else if (propertyMax.propertyType == SerializedPropertyType.Integer)
                    {
                        maxValue = propertyMax.intValue;
                        if (maxValue < 0)
                        {
                            typeError = "valueMax must be positive!";
                        }
                    }
                    else
                    {
                        typeError = "valueMax must referance a number property!";
                    }
                }
                else if (statBar.valueMax is float || statBar.valueMax is int || statBar.valueMax is double)
                {
                    maxValue = (float)statBar.valueMax;
                    if (maxValue < 0)
                    {
                        typeError = "valueMax must be positive!";
                    }
                }
                else
                {
                    typeError = "valueMax must be the name of a property or a number!";
                }
            }
            else
            {
                typeError = "unset";
            }

            float lineHight = EditorGUIUtility.singleLineHeight;
            float padding   = EditorGUIUtility.standardVerticalSpacing;

            Rect positionBar = new Rect(position.position.x, position.position.y, position.size.x,
                                        lineHight);

            float  precentFilled = 0;
            string labelBar      = "";
            bool   error         = false;

            switch (property.propertyType)
            {
            case SerializedPropertyType.Integer:
            {
                if (maxValue < 0)
                {
                    error = true;
                    if (typeError == "unset")
                    {
                        labelBar = "you must provide a valueMax in the StatBarAttribute!";
                    }
                    else
                    {
                        labelBar = typeError;
                    }
                }
                else
                {
                    precentFilled = property.intValue / maxValue;
                    labelBar      = "[" + property.name + "] " + property.intValue + "/" + (int)maxValue;
                }
            } break;

            case SerializedPropertyType.Float:
            {
                if (maxValue < 0)
                {
                    if (property.floatValue > 1)
                    {
                        error = true;
                        if (typeError == "unset")
                        {
                            labelBar = "property value is over 1, and no max value has been specified!";
                        }
                        else
                        {
                            labelBar = typeError;
                        }
                        break;
                    }

                    precentFilled = property.floatValue / 1;
                    labelBar      = "[" + property.name + "] " + (int)property.floatValue + "/" + 1;
                }
                else
                {
                    precentFilled = property.floatValue / maxValue;
                    labelBar      = "[" + property.name + "] " + (int)property.floatValue + "/" + (int)maxValue;
                }
            } break;

            default:
            {
                error    = true;
                labelBar = "unsupported type for a stats bar!";
            } break;
            }

            if (error)
            {
                GUI.Label(positionBar, labelBar);
            }
            else
            {
                Color colorBar   = Colors.FromEnum(statBar.color);
                Color colorLabel = Color.white;
                if (precentFilled < 0)
                {
                    precentFilled = 0;
                }
                if (precentFilled > 1)
                {
                    precentFilled = 1;
                }
                DrawBar(positionBar, precentFilled, labelBar, colorBar, colorLabel);
            }

            EditorGUI.PropertyField(new Rect(position.position.x, position.position.y + lineHight + padding, position.size.x, lineHight), property);
        }