HKI Core
MinMaxRangeDrawer.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEditor;
3 
4 namespace HKI.Core.Variables.Editor
5 {
9  [CustomPropertyDrawer(typeof(MinMaxRangeAttribute))]
10  public class MinMaxRangeDrawer : PropertyDrawer
11  {
12  // OnGUI function
13  public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
14  {
15  // Now draw the property as a Slider or an IntSlider based on whether it’s a float or integer.
16  if (property.type != "MinMaxRange")
17  Debug.LogWarning("Use only with MinMaxRange type");
18  else
19  {
20  MinMaxRangeAttribute range = attribute as MinMaxRangeAttribute;
21  SerializedProperty minValue = property.FindPropertyRelative("RangeStart");
22  SerializedProperty maxValue = property.FindPropertyRelative("RangeEnd");
23  float newMin = minValue.floatValue;
24  float newMax = maxValue.floatValue;
25 
26  float widthLabel = label == null || string.IsNullOrEmpty(label.text) ? 0.0f : position.width * 0.425f;
27  float widthLimit = 30.0f;
28  float widthRange = 50.0f;
29 
30  EditorGUI.LabelField(new Rect(position.x, position.y, widthLabel, position.height), label);
31 
32  EditorGUI.LabelField(new Rect(position.x + widthLabel, position.y, widthLimit, position.height), range.MinLimit.ToString("0.##"));
33 
34  newMin = Mathf.Clamp(EditorGUI.FloatField(new Rect(position.x + widthLabel + widthLimit, position.y, widthRange, position.height), newMin), range.MinLimit, newMax);
35 
36  EditorGUI.MinMaxSlider(new Rect(position.x + widthLabel + widthLimit + widthRange, position.y, position.width - widthLabel - 2 * (widthLimit + widthRange), position.height), ref newMin, ref newMax, range.MinLimit, range.MaxLimit);
37 
38  newMax = Mathf.Clamp(EditorGUI.FloatField(new Rect(position.x + position.width - widthLimit - widthRange, position.y, widthRange, position.height), newMax), newMin, range.MaxLimit);
39 
40  EditorGUI.LabelField(new Rect(position.x + position.width - widthLimit, position.y, widthLimit, position.height), range.MaxLimit.ToString("0.##"));
41 
42  minValue.floatValue = newMin;
43  maxValue.floatValue = newMax;
44  }
45  }
46  }
47 }
Custom property drawer for the MinMaxRangeAttribute class.
This Attribute is for having the use of a special property drawer. In this case for the use of a valu...
override void OnGUI(Rect position, SerializedProperty property, GUIContent label)