Skip to content

Commit 7d7cb55

Browse files
committed
Fix displaying MinMax sliders in indented rects; support for Vector2Int in the MinMaxSliderAttribute
1 parent 4c23c46 commit 7d7cb55

File tree

5 files changed

+85
-23
lines changed

5 files changed

+85
-23
lines changed

Assets/Editor Toolbox/Editor/Drawers/Regular/MinMaxSliderAttributeDrawer.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,49 @@ public class MinMaxSliderAttributeDrawer : PropertyDrawerBase
1010
{
1111
protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
1212
{
13-
return base.GetPropertyHeightSafe(property, label);
13+
return EditorGUIUtility.singleLineHeight;
1414
}
1515

1616
protected override void OnGUISafe(Rect position, SerializedProperty property, GUIContent label)
1717
{
1818
var minValue = Attribute.MinValue;
1919
var maxValue = Attribute.MaxValue;
20-
var xValue = property.vector2Value.x;
21-
var yValue = property.vector2Value.y;
20+
21+
var xValue = 0.0f;
22+
var yValue = 0.0f;
23+
switch (property.propertyType)
24+
{
25+
case SerializedPropertyType.Vector2:
26+
xValue = property.vector2Value.x;
27+
yValue = property.vector2Value.y;
28+
break;
29+
case SerializedPropertyType.Vector2Int:
30+
xValue = property.vector2IntValue.x;
31+
yValue = property.vector2IntValue.y;
32+
break;
33+
}
2234

2335
label = EditorGUI.BeginProperty(position, label, property);
2436
EditorGUI.BeginChangeCheck();
37+
position = EditorGUI.PrefixLabel(position, label);
2538
using (new ZeroIndentScope())
2639
{
27-
ToolboxEditorGui.DrawMinMaxSlider(position, label, ref xValue, ref yValue, minValue, maxValue);
40+
ToolboxEditorGui.DrawMinMaxSlider(position, ref xValue, ref yValue, minValue, maxValue);
2841
}
2942

3043
if (EditorGUI.EndChangeCheck())
3144
{
32-
property.vector2Value = new Vector2(xValue, yValue);
45+
switch (property.propertyType)
46+
{
47+
case SerializedPropertyType.Vector2:
48+
property.vector2Value = new Vector2(xValue, yValue);
49+
break;
50+
case SerializedPropertyType.Vector2Int:
51+
var intXValue = Mathf.RoundToInt(xValue);
52+
var intYValue = Mathf.RoundToInt(yValue);
53+
property.vector2IntValue = new Vector2Int(intXValue, intYValue);
54+
break;
55+
}
3356
}
3457

3558
EditorGUI.EndProperty();
@@ -38,7 +61,8 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
3861

3962
public override bool IsPropertyValid(SerializedProperty property)
4063
{
41-
return property.propertyType == SerializedPropertyType.Vector2;
64+
return property.propertyType == SerializedPropertyType.Vector2 ||
65+
property.propertyType == SerializedPropertyType.Vector2Int;
4266
}
4367

4468

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
1-
using UnityEditor;
1+
using UnityEditor;
22
using UnityEngine;
33

44
namespace Toolbox.Editor.Drawers
55
{
6+
using Toolbox.Editor.Internal;
7+
68
public class DynamicMinMaxSliderAttributeDrawer : DynamicMinMaxBaseDrawer<DynamicMinMaxSliderAttribute>
79
{
810
protected override void OnGuiSafe(SerializedProperty property, GUIContent label, float minValue, float maxValue)
911
{
10-
var xValue = property.vector2Value.x;
11-
var yValue = property.vector2Value.y;
12+
var xValue = 0.0f;
13+
var yValue = 0.0f;
14+
switch (property.propertyType)
15+
{
16+
case SerializedPropertyType.Vector2:
17+
xValue = property.vector2Value.x;
18+
yValue = property.vector2Value.y;
19+
break;
20+
case SerializedPropertyType.Vector2Int:
21+
xValue = property.vector2IntValue.x;
22+
yValue = property.vector2IntValue.y;
23+
break;
24+
}
25+
1226
ToolboxEditorGui.BeginProperty(property, ref label, out var position);
27+
position = EditorGUI.PrefixLabel(position, label);
1328
EditorGUI.BeginChangeCheck();
14-
ToolboxEditorGui.DrawMinMaxSlider(position, label, ref xValue, ref yValue, minValue, maxValue);
29+
using (new ZeroIndentScope())
30+
{
31+
ToolboxEditorGui.DrawMinMaxSlider(position, ref xValue, ref yValue, minValue, maxValue);
32+
}
33+
1534
if (EditorGUI.EndChangeCheck())
1635
{
17-
property.vector2Value = new Vector2(xValue, yValue);
36+
switch (property.propertyType)
37+
{
38+
case SerializedPropertyType.Vector2:
39+
property.vector2Value = new Vector2(xValue, yValue);
40+
break;
41+
case SerializedPropertyType.Vector2Int:
42+
var intXValue = Mathf.RoundToInt(xValue);
43+
var intYValue = Mathf.RoundToInt(yValue);
44+
property.vector2IntValue = new Vector2Int(intXValue, intYValue);
45+
break;
46+
}
1847
}
1948

2049
ToolboxEditorGui.CloseProperty();
@@ -23,7 +52,8 @@ protected override void OnGuiSafe(SerializedProperty property, GUIContent label,
2352

2453
public override bool IsPropertyValid(SerializedProperty property)
2554
{
26-
return property.propertyType == SerializedPropertyType.Vector2;
55+
return property.propertyType == SerializedPropertyType.Vector2 ||
56+
property.propertyType == SerializedPropertyType.Vector2Int;
2757
}
2858
}
2959
}

Assets/Editor Toolbox/Editor/ToolboxEditorGui.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,14 @@ public static void DrawTexture(Rect rect, Texture texture, ScaleMode scaleMode,
199199
GUI.DrawTexture(rect, texture, scaleMode, alphaBlend);
200200
}
201201

202-
public static void DrawMinMaxSlider(Rect rect, string label, ref float xValue, ref float yValue, float minValue, float maxValue)
202+
public static void DrawMinMaxSlider(Rect rect, ref float xValue, ref float yValue, float minValue, float maxValue)
203203
{
204-
DrawMinMaxSlider(rect, new GUIContent(label), ref xValue, ref yValue, minValue, maxValue);
205-
}
206-
207-
public static void DrawMinMaxSlider(Rect rect, GUIContent label, ref float xValue, ref float yValue, float minValue, float maxValue)
208-
{
209-
rect = EditorGUI.PrefixLabel(rect, label);
210-
211204
var fieldWidth = EditorGUIUtility.fieldWidth;
212205
var minFieldRect = new Rect(rect.xMin, rect.y, fieldWidth, rect.height);
213206
var maxFieldRect = new Rect(rect.xMax - fieldWidth, rect.y, fieldWidth, rect.height);
214207

215208
//set slider rect between min and max fields + additional padding
216-
var spacing = 8.0f;
209+
const float spacing = 8.0f;
217210
var sliderRect = Rect.MinMaxRect(minFieldRect.xMax + spacing,
218211
rect.yMin,
219212
maxFieldRect.xMin - spacing,
@@ -229,6 +222,17 @@ public static void DrawMinMaxSlider(Rect rect, GUIContent label, ref float xValu
229222
yValue = Mathf.Clamp(yValue, Mathf.Max(minValue, xValue), maxValue);
230223
}
231224

225+
public static void DrawMinMaxSlider(Rect rect, string label, ref float xValue, ref float yValue, float minValue, float maxValue)
226+
{
227+
DrawMinMaxSlider(rect, new GUIContent(label), ref xValue, ref yValue, minValue, maxValue);
228+
}
229+
230+
public static void DrawMinMaxSlider(Rect rect, GUIContent label, ref float xValue, ref float yValue, float minValue, float maxValue)
231+
{
232+
rect = EditorGUI.PrefixLabel(rect, label);
233+
DrawMinMaxSlider(rect, ref xValue, ref yValue, minValue, maxValue);
234+
}
235+
232236
public static void BoldLabel(Rect rect, string label)
233237
{
234238
BoldLabel(rect, new GUIContent(label));

Assets/Examples/Scenes/SampleScene.unity

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,8 @@ MonoBehaviour:
10091009
m_EditorClassIdentifier:
10101010
targetTag: Untagged
10111011
progressBar: 25.4
1012-
var2: {x: 30.418846, y: 60.78535}
1012+
minMaxVector: {x: 0, y: 0}
1013+
minMaxVectorInt: {x: 0, y: 0}
10131014
var8: {fileID: 977748987}
10141015
preview: {fileID: 5059060190599569102, guid: 5573ca52cac7c2d4cb2536e37e9be1f1, type: 3}
10151016
var10: 2.16
@@ -1027,6 +1028,7 @@ MonoBehaviour:
10271028
prefabReference: {fileID: 0}
10281029
bigNumber: 12345678
10291030
currency: 20.41
1031+
veryVeryVeryVeryVeryLongName: 0
10301032
--- !u!4 &959025299
10311033
Transform:
10321034
m_ObjectHideFlags: 2

Assets/Examples/Scripts/SampleBehaviour1.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public class SampleBehaviour1 : MonoBehaviour
1717
[Label("MinMax Slider", skinStyle: SkinStyle.Box)]
1818

1919
[MinMaxSlider(10.0f, 100.0f)]
20-
public Vector2 var2;
20+
public Vector2 minMaxVector;
21+
[MinMaxSlider(1, 8)]
22+
public Vector2Int minMaxVectorInt;
2123

2224
[Label("Asset Preview", skinStyle: SkinStyle.Box)]
2325

0 commit comments

Comments
 (0)