-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMinMaxDrawer.cs
168 lines (139 loc) · 5.72 KB
/
MinMaxDrawer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using cmdwtf.UnityTools.Attributes;
using UnityEngine;
using UnityEditor;
namespace cmdwtf.UnityTools.Editor
{
// Thanks, Lotte https://gist.github.com/LotteMakesStuff/0de9be35044bab97cbe79b9ced695585
[UnityEditor.CustomPropertyDrawer(typeof(MinMaxAttribute))]
public class MinMaxDrawer : CustomPropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// cast the attribute to make life easier
if (!(attribute is MinMaxAttribute minMax))
{
return;
}
// This only works on a vector2 and vector2Int! 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!
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;
float[]
vals = new float[]
{
minLimit, minValue, maxValue, maxLimit
}; // shove the values and limits into a vector4 and draw them all at once
EditorGUI.MultiFloatField(position, new GUIContent("Range"),
new GUIContent[]
{
new GUIContent("MinLimit"), new GUIContent("MinVal"),
new GUIContent("MaxVal"), new GUIContent("MaxLimit")
}, vals);
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(vals[1], vals[2]); // save off any change to the value~
}
}
}
else if (property.propertyType == SerializedPropertyType.Vector2Int)
{
// 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.vector2IntValue.x; // the currently set minimum and maximum value
float maxValue = property.vector2IntValue.y;
int minLimit =
Mathf.RoundToInt(
minMax.MinLimit); // the limit for both min and max, min cant go lower than minLimit and maax cant top maxLimit
int maxLimit = Mathf.RoundToInt(minMax.MaxLimit);
// and ask unity to draw them all nice for us!
EditorGUI.MinMaxSlider(position, label, ref minValue, ref maxValue, minLimit, maxLimit);
var vec = Vector2Int.zero; // save the results into the property!
vec.x = Mathf.RoundToInt(minValue);
vec.y = Mathf.RoundToInt(maxValue);
property.vector2IntValue = 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;
float[]
vals = new float[]
{
minLimit, minValue, maxValue, maxLimit
}; // shove the values and limits into a vector4 and draw them all at once
EditorGUI.MultiFloatField(position, new GUIContent("Range"),
new GUIContent[]
{
new GUIContent("MinLimit"), new GUIContent("MinVal"),
new GUIContent("MaxVal"), new GUIContent("MaxLimit")
}, vals);
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.vector2IntValue =
new Vector2Int(Mathf.RoundToInt(vals[1]),
Mathf.RoundToInt(vals[2])); // save off any change to the value~
}
}
}
}
protected override int GetPropertyLineCount(SerializedProperty property, GUIContent label)
{
if (!(attribute is MinMaxAttribute minMax))
{
return 0;
}
int lineCount = 1;
// if we have a special mode, add two extra lines!
if (minMax.ShowEditRange || minMax.ShowDebugValues)
{
lineCount += 2;
}
return lineCount;
}
}
}