Inheritance: PropertyAttribute
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        if (property.type != "MinMaxRange")
        {
            Debug.LogWarning("Use only with MinMaxRange type");
        }
        else
        {
            MinMaxRangeAttribute range = attribute as MinMaxRangeAttribute;
            var   minValue             = property.FindPropertyRelative("minimumValue");
            var   maxValue             = property.FindPropertyRelative("maximumValue");
            float newMin = minValue.floatValue;
            float newMax = maxValue.floatValue;

            float xPosDifference = position.width * widthFactor;
            float yPosDifference = position.height * heightFactor;

            EditorGUI.LabelField(new Rect(position.x, position.y, xPosDifference, yPosDifference), label);
            EditorGUI.LabelField(new Rect(position.x, position.y + yPosDifference, position.width, yPosDifference), range.minLimit.ToString(rangeLabelFormat));
            EditorGUI.LabelField(new Rect(position.x + position.width - minSpacing, position.y + yPosDifference, position.width, yPosDifference), range.maxLimit.ToString(rangeLabelFormat));
            EditorGUI.MinMaxSlider(new Rect(position.x + minSpacing, position.y + yPosDifference, position.width - (minSpacing * 2.0f), yPosDifference), ref newMin, ref newMax, range.minLimit, range.maxLimit);

            EditorGUI.LabelField(new Rect(position.x + xPosDifference, position.y, xPosDifference, yPosDifference), "MIN:");
            newMin = Mathf.Clamp(EditorGUI.FloatField(new Rect(position.x + xPosDifference + minSpacing, position.y, xPosDifference - minSpacing, yPosDifference), newMin), range.minLimit, newMax);
            EditorGUI.LabelField(new Rect(position.x + xPosDifference * 2.0f, position.y, xPosDifference, yPosDifference), "MAX:");
            newMax = Mathf.Clamp(EditorGUI.FloatField(new Rect(position.x + xPosDifference * 2.0f + maxSpacing, position.y, xPosDifference - maxSpacing, yPosDifference), newMax), newMin, range.maxLimit);

            minValue.floatValue = newMin;
            maxValue.floatValue = newMax;
        }
    }
Exemple #2
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var range = (MinMaxRangeAttribute)attribute;

            if (range == null)
            {
                range = new MinMaxRangeAttribute(0, 1);
            }

            var min = property.FindPropertyRelative(range.minProperty);
            var max = property.FindPropertyRelative(range.maxProperty);

            if (min == null || max == null)
            {
                EditorGUI.LabelField(position, property.name + "\nCannot find fields. \"" + range.minProperty + "\", \"" + range.maxProperty + "\"");
                return;
            }

            var drawMinMaxSlider = GetDrawSliderFunc(min.propertyType);

            var layout = new PropertyLayoutHelper();

            layout.Add((rect) => { EditorGUI.LabelField(rect, property.name); });
            layout.Begin();
            layout.Add((rect) => EditorGUI.PropertyField(rect, min, GUIContent.none), labelLength);
            layout.Add((rect) => drawMinMaxSlider(rect, min, max, range));
            layout.Add((rect) => EditorGUI.PropertyField(rect, max, GUIContent.none), labelLength);
            layout.End();
            layout.Render(position);
        }
Exemple #3
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (property.propertyType != SerializedPropertyType.Vector2)
            {
                Debug.LogError($"{nameof(MinMaxRangeAttribute)} is only supported on fields of type Vector2.");
                EditorGUI.PropertyField(position, property, label, true);
                return;
            }

            MinMaxRangeAttribute attr = (MinMaxRangeAttribute)attribute;
            Vector2 value             = property.vector2Value;

            label = EditorGUI.BeginProperty(position, label, property);

            position = EditorGUI.PrefixLabel(position, label);
            Rect minFieldPos = position;
            Rect maxFieldPos = position;
            Rect sliderPos   = position;

            minFieldPos.xMax = minFieldPos.xMin + FieldSize;
            maxFieldPos.xMin = maxFieldPos.xMax - FieldSize;
            sliderPos.xMin   = minFieldPos.xMax + FieldMargin;
            sliderPos.xMax   = maxFieldPos.xMin - FieldMargin;

            value.x = Mathf.Clamp(EditorGUI.FloatField(minFieldPos, value.x), attr.Min, value.y);
            EditorGUI.MinMaxSlider(sliderPos, ref value.x, ref value.y, attr.Min, attr.Max);
            value.y = Mathf.Clamp(EditorGUI.FloatField(maxFieldPos, value.y), value.x, attr.Max);

            EditorGUI.EndProperty();

            property.vector2Value = value;
        }
        public override bool OnGUI(SerializedDataParameter property, GUIContent title)
        {
            //MinMaxRangeAttribute range = attribute as MinMaxRangeAttribute;
            MinMaxRangeAttribute range    = property.GetAttribute <MinMaxRangeAttribute>();
            SerializedProperty   minRange = property.value.FindPropertyRelative("minValue");
            SerializedProperty   maxRange = property.value.FindPropertyRelative("maxValue");

            float minValue = minRange.floatValue;
            float maxValue = maxRange.floatValue;

            Rect startRect = EditorGUILayout.GetControlRect();

            Rect  minRect    = new Rect(EditorGUIUtility.labelWidth + EditorGUIUtility.fieldWidth * 0.75f, startRect.y, EditorGUIUtility.fieldWidth, startRect.height);
            float p          = minRect.x + EditorGUIUtility.standardVerticalSpacing * 2f + EditorGUIUtility.fieldWidth;
            Rect  sliderRect = new Rect(p, startRect.y, EditorGUIUtility.currentViewWidth - p - EditorGUIUtility.fieldWidth * 1.5f, startRect.height);
            Rect  maxRect    = new Rect(sliderRect.x + sliderRect.width + EditorGUIUtility.standardVerticalSpacing * 2f, startRect.y, EditorGUIUtility.fieldWidth, startRect.height);

            EditorGUI.LabelField(startRect, title.text);
            minValue = EditorGUI.FloatField(minRect, minValue);

            EditorGUI.MinMaxSlider(sliderRect, ref minValue, ref maxValue, range.minLimit, range.maxLimit);
            maxValue = EditorGUI.FloatField(maxRect, maxValue);

            minRange.floatValue = minValue;
            maxRange.floatValue = maxValue;

            return(true);
        }
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            // cast the attribute to make life easier
            MinMaxRangeAttribute minMax = attribute as MinMaxRangeAttribute;

            // This only works on a vector2! ignore on any other property type (we should probably draw an error message instead!)
            if (property.propertyType == SerializedPropertyType.Vector2)
            {
                // if we are flagged to draw in a special mode, lets modify the drawing rectangle to draw only one line at a time
                if (minMax.ShowDebugValues || minMax.ShowEditRange)
                {
                    position = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
                }

                // pull out a bunch of helpful min/max values....
                float minValue = property.vector2Value.x; // the currently set minimum and maximum value
                float maxValue = property.vector2Value.y;
                float minLimit = minMax.MinLimit;         // the limit for both min and max, min cant go lower than minLimit and maax cant top maxLimit
                float maxLimit = minMax.MaxLimit;

                // and ask unity to draw them all nice for us!
                EditorGUI.MinMaxSlider(position, label, ref minValue, ref maxValue, minLimit, maxLimit);

                var vec = Vector2.zero; // save the results into the property!
                vec.x = minValue;
                vec.y = maxValue;

                property.vector2Value = vec;

                // Do we have a special mode flagged? time to draw lines!
                if (minMax.ShowDebugValues || minMax.ShowEditRange)
                {
                    bool isEditable = minMax.ShowEditRange;

                    if (!isEditable)
                    {
                        GUI.enabled = false; // if were just in debug mode and not edit mode, make sure all the UI is read only!
                    }
                    // move the draw rect on by one line
                    position.y += EditorGUIUtility.singleLineHeight;

                    Vector4 val = new Vector4(minLimit, minValue, maxValue, maxLimit); // shove the values and limits into a vector4 and draw them all at once
                    val = EditorGUI.Vector4Field(position, "MinLimit/MinVal/MaxVal/MaxLimit", val);

                    GUI.enabled = false; // the range part is always read only
                    position.y += EditorGUIUtility.singleLineHeight;
                    EditorGUI.FloatField(position, "Selected Range", maxValue - minValue);
                    GUI.enabled = true; // remember to make the UI editable again!

                    if (isEditable)
                    {
                        property.vector2Value = new Vector2(val.y, val.z); // save off any change to the value~
                    }
                }
            }
        }
Exemple #6
0
        public void DrawDirectly(SerializedProperty property, CyberAttribute atribute, GUIContent content, GUIStyle style, FieldInfo field)
        {
            MinMaxRangeAttribute atr = atribute as MinMaxRangeAttribute;


            EditorGUILayout.BeginHorizontal();
            TheEditor.DrawPrefix(content, field, style);
            property.SetValue(EditorGUILayout.Slider((float)Convert.ChangeType(property.GetJustValue(), typeof(float)), atr.Min, atr.Max));
            EditorGUILayout.EndHorizontal();
        }
        // Min-max slider credit: https://github.com/Unity-Technologies/UnityCsReference/blob/61f92bd79ae862c4465d35270f9d1d57befd1761/Editor/Mono/Inspector/LightEditor.cs#L328-L363
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            MinMaxRangeAttribute minMaxRange = attribute as MinMaxRangeAttribute;

            SerializedProperty minProp = property.FindPropertyRelative("x");
            SerializedProperty maxProp = property.FindPropertyRelative("y");

            position = EditorGUI.PrefixLabel(position, label);
            EditorGUI.BeginProperty(position, GUIContent.none, property);

            Rect minRect = new Rect(position)
            {
                width = MIN_MAX_SLIDER_TEXT_FIELD_WIDTH
            };
            Rect maxRect = new Rect(position)
            {
                xMin = position.xMax - MIN_MAX_SLIDER_TEXT_FIELD_WIDTH
            };
            Rect sliderRect = new Rect(position)
            {
                xMin = minRect.xMax + 5f, xMax = maxRect.xMin - 5f
            };

            EditorGUI.BeginChangeCheck();

            EditorGUI.PropertyField(minRect, minProp, GUIContent.none);

            Vector2 value = property.vector2Value;

            EditorGUI.BeginChangeCheck();
            EditorGUI.MinMaxSlider(sliderRect, ref value.x, ref value.y, minMaxRange.min, minMaxRange.max);
            if (EditorGUI.EndChangeCheck())
            {
                property.vector2Value = value;
            }

            EditorGUI.PropertyField(maxRect, maxProp, GUIContent.none);

            if (EditorGUI.EndChangeCheck())
            {
                float x = minProp.floatValue;
                float y = maxProp.floatValue;

                if (x < minMaxRange.min || x > minMaxRange.max)
                {
                    minProp.floatValue = Mathf.Clamp(x, minMaxRange.min, minMaxRange.max);
                }
                if (y < minMaxRange.min || y > minMaxRange.max)
                {
                    maxProp.floatValue = Mathf.Clamp(y, minMaxRange.min, minMaxRange.max);
                }
            }

            EditorGUI.EndProperty();
        }
Exemple #8
0
    //public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    //{
    //    return EditorGUIUtility.singleLineHeight;
    //}

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        MinMaxRangeAttribute attr    = (MinMaxRangeAttribute)attribute;
        SerializedProperty   minProp = property.FindPropertyRelative("min");
        SerializedProperty   maxProp = property.FindPropertyRelative("max");

        label = EditorGUI.BeginProperty(position, label, property);
        float min = (float)minProp.intValue;
        float max = (float)maxProp.intValue;

        EditorGUI.MinMaxSlider(position, label, ref min, ref max, attr.min, attr.max);
        minProp.intValue = (int)min;
        maxProp.intValue = (int)max;
        EditorGUI.EndProperty();
    }
        // this method lets unity know how big to draw the property. We need to override this because it could end up meing more than one line big
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            MinMaxRangeAttribute minMax = attribute as MinMaxRangeAttribute;

            // by default just return the standard line height
            float size = EditorGUIUtility.singleLineHeight;

            // if we have a special mode, add two extra lines!
            if (minMax.ShowEditRange || minMax.ShowDebugValues)
            {
                size += EditorGUIUtility.singleLineHeight * 2;
            }

            return(size);
        }
    public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
    {
        if (property.type != "Vector2")
        {
            EditorGUI.LabelField(rect, label, "Error: MinMaxRange requires a Vector2");
        }
        else
        {
            MinMaxRangeAttribute range = attribute as MinMaxRangeAttribute;
            var minProperty            = property.FindPropertyRelative("x");
            var maxProperty            = property.FindPropertyRelative("y");

            EditorGUI.PrefixLabel(rect, label);
            label.text = "";

            //++EditorGUI.indentLevel;
            var indent = EditorGUI.indentLevel * 14f;

            EditorGUI.PropertyField(new Rect(rect.x, rect.y + 16f, 50f + indent, 16f), minProperty, label);
            label.text = "";
            EditorGUI.PropertyField(new Rect(rect.x + rect.width - 50f - indent, rect.y + 16f, 50f + indent, 16f), maxProperty, label);

            float min = minProperty.floatValue;
            float max = maxProperty.floatValue;

            GUI.changed = false;
            EditorGUI.MinMaxSlider(new Rect(rect.x + 60f, rect.y + 16f, rect.width - 120f, 16f), ref min, ref max, range.minLimit, range.maxLimit);
            //--EditorGUI.indentLevel;

            if (GUI.changed)
            {
                minProperty.floatValue = min;
                maxProperty.floatValue = max;
            }
        }
    }
Exemple #11
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // Using BeginProperty / EndProperty on the parent property means that
        // prefab override logic works on the entire property.
        EditorGUI.BeginProperty(position, label, property);

        // Draw label
        position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);

        // Don't make child fields be indented
        var indent = EditorGUI.indentLevel;

        EditorGUI.indentLevel = 0;

        MinMaxRangeAttribute range       = attribute as MinMaxRangeAttribute;
        SerializedProperty   minProperty = property.FindPropertyRelative("m_Min");
        SerializedProperty   maxProperty = property.FindPropertyRelative("m_Max");

        // Calculate rects
        Rect minRect    = new Rect(position.x, position.y, 50, position.height);
        Rect maxRect    = new Rect(position.x + position.width - 50, position.y, 50, position.height);
        Rect sliderRect = new Rect(position.x + 55, position.y, position.width - 110, position.height);

        // Draw fields - passs GUIContent.none to each so they are drawn without labels
        float minValue = EditorGUI.FloatField(minRect, minProperty.floatValue);
        float maxValue = EditorGUI.FloatField(maxRect, maxProperty.floatValue);

        EditorGUI.MinMaxSlider(sliderRect, ref minValue, ref maxValue, range.MinLimit, range.MaxLimit);

        // Set indent back to what it was
        EditorGUI.indentLevel = indent;
        EditorGUI.EndProperty();

        minProperty.floatValue = minValue;
        maxProperty.floatValue = maxValue;
    }
Exemple #12
0
        public override void OnGUI(Rect startRect, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(startRect, label, property);
            MinMaxRangeAttribute range    = attribute as MinMaxRangeAttribute;
            SerializedProperty   minRange = property.FindPropertyRelative("minValue");
            SerializedProperty   maxRange = property.FindPropertyRelative("maxValue");
            float minValue = minRange.floatValue;
            float maxValue = maxRange.floatValue;

            Rect  minRect    = new Rect(EditorGUIUtility.labelWidth + EditorGUIUtility.fieldWidth * 0.33f, startRect.y, EditorGUIUtility.fieldWidth, startRect.height);
            float p          = minRect.x + EditorGUIUtility.standardVerticalSpacing * 2f + EditorGUIUtility.fieldWidth;
            Rect  sliderRect = new Rect(p, startRect.y, startRect.width - p - EditorGUIUtility.fieldWidth + EditorGUIUtility.standardVerticalSpacing * 5f, startRect.height);
            Rect  maxRect    = new Rect(sliderRect.x + sliderRect.width + EditorGUIUtility.standardVerticalSpacing * 2f, startRect.y, EditorGUIUtility.fieldWidth, startRect.height);

            EditorGUI.LabelField(startRect, label);
            minValue = EditorGUI.FloatField(minRect, minValue);

            EditorGUI.MinMaxSlider(sliderRect, ref minValue, ref maxValue, range.minLimit, range.maxLimit);
            maxValue = EditorGUI.FloatField(maxRect, maxValue);

            minRange.floatValue = minValue;
            maxRange.floatValue = maxValue;
            EditorGUI.EndProperty();
        }
Exemple #13
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            SerializedProperty minProp = property.FindPropertyRelative("Min");
            SerializedProperty maxProp = property.FindPropertyRelative("Max");

            if (minProp == null || maxProp == null)
            {
                WarningsPool.Log("MinMaxRangeAttribute used on <color=brown>" +
                                 property.name +
                                 "</color>. Must be used on types with Min and Max fields",
                                 property.serializedObject.targetObject);

                return;
            }

            var minValid = minProp.propertyType == SerializedPropertyType.Integer || minProp.propertyType == SerializedPropertyType.Float;
            var maxValid = maxProp.propertyType == SerializedPropertyType.Integer || maxProp.propertyType == SerializedPropertyType.Float;

            if (!maxValid || !minValid || minProp.propertyType != maxProp.propertyType)
            {
                WarningsPool.Log("MinMaxRangeAttribute used on <color=brown>" +
                                 property.name +
                                 "</color>. Min and Max fields must be of int or float type",
                                 property.serializedObject.targetObject);

                return;
            }

            MinMaxRangeAttribute rangeAttribute = (MinMaxRangeAttribute)attribute;

            label    = EditorGUI.BeginProperty(position, label, property);
            position = EditorGUI.PrefixLabel(position, label);

            bool isInt = minProp.propertyType == SerializedPropertyType.Integer;

            float minValue = isInt ? minProp.intValue : minProp.floatValue;
            float maxValue = isInt ? maxProp.intValue : maxProp.floatValue;
            float rangeMin = rangeAttribute.Min;
            float rangeMax = rangeAttribute.Max;


            const float rangeBoundsLabelWidth = 40f;

            var rangeBoundsLabel1Rect = new Rect(position);

            rangeBoundsLabel1Rect.width = rangeBoundsLabelWidth;
            GUI.Label(rangeBoundsLabel1Rect, new GUIContent(minValue.ToString(isInt ? "F0" : "F2")));
            position.xMin += rangeBoundsLabelWidth;

            var rangeBoundsLabel2Rect = new Rect(position);

            rangeBoundsLabel2Rect.xMin = rangeBoundsLabel2Rect.xMax - rangeBoundsLabelWidth;
            GUI.Label(rangeBoundsLabel2Rect, new GUIContent(maxValue.ToString(isInt ? "F0" : "F2")));
            position.xMax -= rangeBoundsLabelWidth;

            EditorGUI.BeginChangeCheck();
            EditorGUI.MinMaxSlider(position, ref minValue, ref maxValue, rangeMin, rangeMax);

            if (EditorGUI.EndChangeCheck())
            {
                if (isInt)
                {
                    minProp.intValue = Mathf.RoundToInt(minValue);
                    maxProp.intValue = Mathf.RoundToInt(maxValue);
                }
                else
                {
                    minProp.floatValue = minValue;
                    maxProp.floatValue = maxValue;
                }
            }

            EditorGUI.EndProperty();
        }
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        MinMaxRangeAttribute range = attribute as MinMaxRangeAttribute;

        if (property.propertyType == SerializedPropertyType.Vector2)
        {
            Vector2 vec;
            if (property.vector2Value.x < range.minLimit || property.vector2Value.y > range.maxLimit)
            {
                float middle = (range.minLimit + range.maxLimit) / 2f;
                vec.x = middle;
                vec.y = middle;
                property.vector2Value = vec;
                return;
            }
            if (range.useMiddle)
            {
                vec = property.vector2Value;
                bool changed = false;
                if (vec.x > range.middle)
                {
                    vec.x   = range.middle;
                    changed = true;
                }
                if (vec.y < range.middle)
                {
                    vec.y   = range.middle;
                    changed = true;
                }
                if (changed)
                {
                    property.vector2Value = vec;
                }
            }
            float x = property.vector2Value.x, y = property.vector2Value.y;
            position.height = 16f;
            EditorGUI.MinMaxSlider(label, position, ref x, ref y, range.minLimit, range.maxLimit);
            vec.x = x;
            vec.y = y;
            property.vector2Value = vec;
            position.y           += 20f;
            vec = EditorGUI.Vector2Field(position, "--->", property.vector2Value);

            if (vec.x < range.minLimit)
            {
                vec.x = range.minLimit;
            }
            if (vec.y > range.maxLimit)
            {
                vec.y = range.maxLimit;
            }
            if (range.useMiddle)
            {
                if (vec.x > range.middle)
                {
                    vec.x = range.middle;
                }
                if (vec.y < range.middle)
                {
                    vec.y = range.middle;
                }
            }
            else if (vec.y < vec.x)
            {
                vec.y = vec.x;
            }

            property.vector2Value = vec;
        }
    }
Exemple #15
0
        private void MinMaxSliderInt(Rect position, SerializedProperty min, SerializedProperty max, MinMaxRangeAttribute range)
        {
            var minValue = (float)min.intValue;
            var maxValue = (float)max.intValue;

            EditorGUI.MinMaxSlider(position, ref minValue, ref maxValue, range.min, range.max);
            min.intValue = Mathf.FloorToInt(minValue);
            max.intValue = Mathf.CeilToInt(maxValue);
        }
Exemple #16
0
        private void MinMaxSlider(Rect position, SerializedProperty min, SerializedProperty max, MinMaxRangeAttribute range)
        {
            var minValue = min.floatValue;
            var maxValue = max.floatValue;

            EditorGUI.MinMaxSlider(position, ref minValue, ref maxValue, range.min, range.max);
            min.floatValue = minValue;
            max.floatValue = maxValue;
        }
Exemple #17
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // cast the attribute to make life easier
        MinMaxRangeAttribute minMax = attribute as MinMaxRangeAttribute;

        if (property.propertyType == SerializedPropertyType.Vector2)
        {
            isInteger = false;
        }
        else if (property.propertyType == SerializedPropertyType.Vector2Int)
        {
            isInteger = true;
        }
        else
        {
            EditorGUI.LabelField(position, label.text, "Use MinMaxRange with Vector2 or Vector2Int");
            return;
        }
        // ---
        position = EditorGUI.PrefixLabel(position, label); // Write label and move position
        // if we are flagged to draw in a special mode, lets modify the drawing rectangle to draw only one line at a time
        if (minMax.displaySize || minMax.editableRange)
        {
            position = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
        }
        // pull out a bunch of helpful min/max values....
        minValue = isInteger ? property.vector2IntValue.x : property.vector2Value.x;
        maxValue = isInteger ? property.vector2IntValue.y : property.vector2Value.y;
        // the limit for both min and max, min cant go lower than minLimit and max cant top maxLimit
        minLimit = isInteger ? (int)minMax.MinLimit : minMax.MinLimit;
        maxLimit = isInteger ? (int)minMax.MaxLimit : minMax.MaxLimit;
        //
        EditorGUI.BeginChangeCheck();
        float indent = (EditorGUI.indentLevel + 0) * 14f;

        position.x     = position.x - indent;
        position.xMax += indent;
        float labelWidth   = position.x * .36f + indent; //40f;
        float labelPadding = position.x * .02f + indent; //4f;
        // - Left Label showing Min Value
        Rect leftLabelRect = new Rect(position);

        leftLabelRect.width = labelWidth;
        {
            minValue = IntFloatField(isInteger, minValue, leftLabelRect);
            minValue = Mathf.Clamp(minValue, minLimit, maxValue);
        }
        position.xMin += (labelWidth - labelPadding) * 1.1f;
        // - Right Label
        Rect rightLabelRect = new Rect(position);

        rightLabelRect.xMin = rightLabelRect.xMax - labelWidth;
        {
            maxValue = IntFloatField(isInteger, maxValue, rightLabelRect);
            maxValue = Mathf.Clamp(maxValue, minValue, maxLimit);
        }
        position.xMax -= (labelWidth - labelPadding) * 1.1f;

        // and ask unity to draw them all nice for us!
        EditorGUI.MinMaxSlider(position, GUIContent.none, ref minValue, ref maxValue, minLimit, maxLimit);
        position.xMin -= labelWidth - labelPadding;
        position.xMax += labelWidth - labelPadding;
        // -
        float[] newFloatRange = { minLimit, maxLimit };
        int[]   newIntRange   = { (int)minLimit, (int)maxLimit };
        if (minMax.editableRange)
        {
            position.y += EditorGUIUtility.singleLineHeight + 2f;
            GUIContent[] content =
            {
                new GUIContent {
                    text = "m", tooltip = "Minimum Limit"
                },
                new GUIContent {
                    text = "M", tooltip = "Maximum Limit"
                }
            };
            if (isInteger)
            {
                EditorGUI.MultiIntField(position, content, newIntRange);
            }
            else
            {
                EditorGUI.MultiFloatField(position, content, newFloatRange);
            }
        }
        // save the results into the property!
        if (EditorGUI.EndChangeCheck())
        {
            if (isInteger)
            {
                property.vector2IntValue = new Vector2Int((int)minValue, (int)maxValue);
                // - Limits modification
                float newMinLimit = Mathf.Min(newIntRange[0], minValue);
                float newMaxLimit = Mathf.Max(newIntRange[1], maxValue);
                minMax.SetMinLimit(newMinLimit);
                minMax.SetMaxLimit(newMaxLimit);
            }
            else
            {
                property.vector2Value = new Vector2(minValue, maxValue);
                // - Limits modification
                float newMinLimit = Mathf.Min(newFloatRange[0], minValue);
                float newMaxLimit = Mathf.Max(newFloatRange[1], maxValue);
                minMax.SetMinLimit(newMinLimit);
                minMax.SetMaxLimit(newMaxLimit);
            }
        }
        // - Display Range Size
        if (minMax.displaySize)
        {
            position.y += EditorGUIUtility.singleLineHeight;
            position.y -= 2;     // So it looks centered but tiny
            GUI.enabled = false; // the range part is always read only
            EditorGUI.LabelField(position,
                                 "Range size: " + (maxValue - minValue).ToString("F4"),
                                 EditorStyles.miniLabel);
            GUI.enabled = true; // remember to make the UI editable again!
        }
    }
Exemple #18
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        //base.OnGUI(position, property, label);
        int originalIndentLevel = EditorGUI.indentLevel;

        EditorGUI.BeginProperty(position, label, property);

        EditorGUILayout.Space();
        //EditorGUILayout.LabelField(property.displayName, EditorStyles.boldLabel);

        //  From NaughtyAttributes
        //MinMaxRangeAttribute minMaxSliderAttribute = PropertyUtility.GetAttribute<MinMaxRangeAttribute>(property);
        MinMaxRangeAttribute minMaxSliderAttribute = this.attribute as MinMaxRangeAttribute;


        if (property.propertyType == SerializedPropertyType.Vector2)
        {
            Rect controlRect = EditorGUILayout.GetControlRect();
            //  If indent level is zero, value is infinity.

            //EditorGUI.indentLevel = 0;
            int   indentLevel     = EditorGUI.indentLevel > 0 ? EditorGUI.indentLevel : 0;
            float labelWidth      = EditorGUIUtility.labelWidth;
            float floatFieldWidth = EditorGUIUtility.fieldWidth;
            float positionOffset  = indentLevel > 0 ? floatFieldWidth * (floatFieldWidth / (floatFieldWidth * indentLevel)) * indentLevel : indentLevel;
            float sliderWidth     = controlRect.width - labelWidth - (2 * floatFieldWidth) - indentLevel;
            float sliderPadding   = 5f + indentLevel;

            Rect labelRect = new Rect(
                controlRect.x,
                controlRect.y,
                labelWidth - positionOffset,
                controlRect.height);

            Rect sliderRect = new Rect(
                controlRect.x + labelWidth + floatFieldWidth + sliderPadding - positionOffset,
                controlRect.y,
                sliderWidth - (2f * sliderPadding) + positionOffset,
                controlRect.height);

            Rect minFloatFieldRect = new Rect(
                controlRect.x + labelWidth - positionOffset,
                controlRect.y,
                floatFieldWidth + positionOffset - indentLevel,
                controlRect.height);

            Rect maxFloatFieldRect = new Rect(
                controlRect.x + labelWidth + floatFieldWidth + sliderWidth - positionOffset + indentLevel,
                controlRect.y,
                floatFieldWidth + positionOffset - indentLevel,
                controlRect.height);

            // Draw the label
            EditorGUI.LabelField(labelRect, property.displayName);
            //EditorGUI.LabelField(labelRect, string.Format("{0} | {1} | {2}", property.displayName, floatFieldWidth, floatFieldWidth + positionOffset) );

            // Draw the slider
            EditorGUI.BeginChangeCheck();

            Vector2 sliderValue = property.vector2Value;
            EditorGUI.MinMaxSlider(sliderRect, ref sliderValue.x, ref sliderValue.y, minMaxSliderAttribute.MinValue, minMaxSliderAttribute.MaxValue);

            sliderValue.x = EditorGUI.FloatField(minFloatFieldRect, sliderValue.x);
            sliderValue.x = Mathf.Clamp(sliderValue.x, minMaxSliderAttribute.MinValue, Mathf.Min(minMaxSliderAttribute.MaxValue, sliderValue.y));
            sliderValue.x = (float)Math.Round(sliderValue.x, minMaxSliderAttribute.Round);

            sliderValue.y = EditorGUI.FloatField(maxFloatFieldRect, sliderValue.y);
            sliderValue.y = Mathf.Clamp(sliderValue.y, Mathf.Max(minMaxSliderAttribute.MinValue, sliderValue.x), minMaxSliderAttribute.MaxValue);
            sliderValue.y = (float)Math.Round(sliderValue.y, minMaxSliderAttribute.Round);

            if (EditorGUI.EndChangeCheck())
            {
                property.vector2Value = sliderValue;
            }
        }
        else
        {
            string warning = minMaxSliderAttribute.GetType().Name + " can be used only on Vector2 fields";
            EditorGUILayout.HelpBox(warning, MessageType.Warning);
            EditorGUILayout.PropertyField(property, true);
        }

        EditorGUI.EndProperty();
        EditorGUI.indentLevel = originalIndentLevel;
    }