Exemplo n.º 1
0
        public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
        {
            Type type = property.GetValueType();

            if (type == typeof(FloatRange))
            {
                FloatRangeDrawer.Draw(rect, property, label, true, attribute as ClampAttribute);
            }
            else if (type == typeof(IntRange))
            {
                FloatRangeDrawer.Draw(rect, property, label, false, attribute as ClampAttribute);
            }
            else
            {
                EditorGUI.BeginProperty(rect, label, property);

                EditorGUI.BeginChangeCheck();
                EditorGUI.PropertyField(rect, property, label);

                if (EditorGUI.EndChangeCheck())
                {
                    ClampAttribute clamp = attribute as ClampAttribute;
                    if (property.propertyType == SerializedPropertyType.Float)
                    {
                        property.floatValue = Mathf.Clamp(property.floatValue, clamp.MinFloat, clamp.MaxFloat);
                    }
                    if (property.propertyType == SerializedPropertyType.Integer)
                    {
                        property.intValue = Mathf.Clamp(property.intValue, clamp.MinInt, clamp.MaxInt);
                    }
                }

                EditorGUI.EndProperty();
            }
        }
Exemplo n.º 2
0
        public static void Draw(Rect totalRect, SerializedProperty property, GUIContent label, bool isFloat, ClampAttribute clamp)
        {
            float labelWidth  = EditorGUIUtility.labelWidth;
            int   indentLevel = EditorGUI.indentLevel;

            EditorGUI.BeginProperty(totalRect, label, property);

            Rect rect = EditorGUI.PrefixLabel(totalRect, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(label.text, property.GetTooltip()));
            SerializedProperty minProperty = property.FindPropertyRelative("_min");
            SerializedProperty maxProperty = property.FindPropertyRelative("_max");

            float valueWidth = (rect.width * 0.5f);

            Rect minValueRect = new Rect(rect.xMin, rect.y, valueWidth - 3f, rect.height);
            Rect maxValueRect = new Rect(rect.x + (rect.width * 0.5f), rect.y, valueWidth, rect.height);

            bool changedMin = false;
            bool changedMax = false;


            EditorGUI.indentLevel       = 0;
            EditorGUIUtility.labelWidth = 30f;

            EditorGUI.BeginChangeCheck();
            EditorGUI.PropertyField(minValueRect, minProperty, new GUIContent("Min"));
            if (EditorGUI.EndChangeCheck())
            {
                changedMin = true;
            }


            EditorGUIUtility.labelWidth = 33f;

            EditorGUI.BeginChangeCheck();
            EditorGUI.PropertyField(maxValueRect, maxProperty, new GUIContent("Max"));
            if (EditorGUI.EndChangeCheck())
            {
                changedMax = true;
            }
            EditorGUIUtility.labelWidth = labelWidth;
            EditorGUI.indentLevel       = indentLevel;

            if (changedMin || changedMax)
            {
                if (clamp != null)
                {
                    if (isFloat)
                    {
                        minProperty.floatValue = Mathf.Clamp(minProperty.floatValue, clamp.MinFloat, clamp.MaxFloat);
                        maxProperty.floatValue = Mathf.Clamp(maxProperty.floatValue, clamp.MinFloat, clamp.MaxFloat);
                    }
                    else
                    {
                        minProperty.intValue = Mathf.Clamp(minProperty.intValue, clamp.MinInt, clamp.MaxInt);
                        maxProperty.intValue = Mathf.Clamp(maxProperty.intValue, clamp.MinInt, clamp.MaxInt);
                    }
                }
            }

            if (changedMin)
            {
                if (isFloat && minProperty.floatValue > maxProperty.floatValue)
                {
                    maxProperty.floatValue = minProperty.floatValue;
                }
                if (isFloat && maxProperty.floatValue < minProperty.floatValue)
                {
                    minProperty.floatValue = maxProperty.floatValue;
                }

                if (!isFloat && minProperty.intValue > maxProperty.intValue)
                {
                    maxProperty.intValue = minProperty.intValue;
                }
                if (!isFloat && maxProperty.intValue < minProperty.intValue)
                {
                    minProperty.intValue = maxProperty.intValue;
                }
            }

            if (changedMax)
            {
                if (isFloat && maxProperty.floatValue < minProperty.floatValue)
                {
                    minProperty.floatValue = maxProperty.floatValue;
                }
                if (isFloat && minProperty.floatValue > maxProperty.floatValue)
                {
                    maxProperty.floatValue = minProperty.floatValue;
                }

                if (!isFloat && maxProperty.intValue < minProperty.intValue)
                {
                    minProperty.intValue = maxProperty.intValue;
                }
                if (!isFloat && minProperty.intValue > maxProperty.intValue)
                {
                    maxProperty.intValue = minProperty.intValue;
                }
            }



            EditorGUI.EndProperty();
        }