Search Unity

Min Max Slider

Discussion in 'Immediate Mode GUI (IMGUI)' started by Rclaire, Mar 23, 2019.

  1. Rclaire

    Rclaire

    Joined:
    Jul 2, 2018
    Posts:
    7
    Hi, I'm trying to make a Min Max Slider for a project. I managed to modify a bit a code that I found on Internet and it looks like this: Help1.PNG
    The problem is at image 2: When I have a "tree" of values, the float fields are a lot smaller and the slider too. Do you have any idea how to fix this ?
    Help2.PNG
    Code:
    Code (CSharp):
    1.  
    2. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
    3.  
    4.         if (property.serializedObject.isEditingMultipleObjects) return;
    5.         float textFieldWidth = 100;
    6.  
    7.         Rect sliderPos = position;
    8.         sliderPos.x += EditorGUIUtility.labelWidth + textFieldWidth;
    9.         sliderPos.width -= EditorGUIUtility.labelWidth + textFieldWidth * 2;
    10.  
    11.         var minProperty = property.FindPropertyRelative("min");
    12.         var maxProperty = property.FindPropertyRelative("max");
    13.         var minmax = attribute as MinMaxSliderAttribute ?? new MinMaxSliderAttribute(0, 1);
    14.  
    15.         var min = minProperty.floatValue;
    16.         var max = maxProperty.floatValue;
    17.         EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
    18.  
    19.         Rect minPos = position;
    20.         minPos.x += EditorGUIUtility.labelWidth;
    21.         minPos.width = textFieldWidth;
    22.         min = Mathf.Clamp(EditorGUI.FloatField(minPos, min), minmax.Min, max);
    23.         Rect maxPos = position;
    24.         maxPos.x += maxPos.width - textFieldWidth;
    25.         maxPos.width = textFieldWidth;
    26.         max = Mathf.Clamp(EditorGUI.FloatField(maxPos, max), min, minmax.Max);
    27.  
    28.         EditorGUI.MinMaxSlider(sliderPos, GUIContent.none, ref min, ref max, minmax.Min, minmax.Max);
    29.  
    30.         minProperty.floatValue = min;
    31.         maxProperty.floatValue = max;
    32.     }
    Sorry for my bad english.