Skip to content

Commit 4982f58

Browse files
committed
Added support for sliders.
Use the `[Slider]` attribute in place of `Range` for MinMax support on Vector2 and also labeling support.
1 parent 1f7d369 commit 4982f58

File tree

5 files changed

+156
-1
lines changed

5 files changed

+156
-1
lines changed

Editor/Attributes/SlideDrawer.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using UnityEditor;
2+
using UnityEngine;
3+
4+
namespace Lachee.Attributes.Editor
5+
{
6+
[CustomPropertyDrawer(typeof(SlideAttribute))]
7+
public class SlideDrawer : PropertyDrawer
8+
{
9+
const float miniLabelVerticalOffset = -7;
10+
11+
static GUIStyle miniLabelStyle;
12+
static SlideDrawer()
13+
{
14+
miniLabelStyle = new GUIStyle(EditorStyles.miniLabel);
15+
}
16+
17+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
18+
{
19+
SlideAttribute attr = (SlideAttribute)attribute;
20+
21+
22+
// Draw the left and right
23+
if (attr.LeftLabel != null)
24+
{
25+
miniLabelStyle.alignment = TextAnchor.UpperLeft;
26+
EditorGUI.LabelField(new Rect(position.x + EditorGUIUtility.labelWidth, position.y + miniLabelVerticalOffset, position.width, position.height), attr.LeftLabel, miniLabelStyle);
27+
}
28+
29+
//style.alignment = TextAnchor.LowerRight;
30+
if (attr.RightLabel != null)
31+
{
32+
float padding = property.propertyType == SerializedPropertyType.Float || property.propertyType == SerializedPropertyType.Integer ? -EditorGUIUtility.fieldWidth - 5 : 0;
33+
miniLabelStyle.alignment = TextAnchor.UpperRight;
34+
EditorGUI.LabelField(new Rect(position.x + EditorGUIUtility.labelWidth, position.y + miniLabelVerticalOffset, position.width - EditorGUIUtility.labelWidth + padding, position.height), attr.RightLabel, miniLabelStyle);
35+
}
36+
37+
Rect sliderBox = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
38+
39+
// Draw the slider
40+
switch (property.propertyType)
41+
{
42+
case SerializedPropertyType.Float:
43+
EditorGUI.Slider(sliderBox, property, attr.Min, attr.Max, label);
44+
break;
45+
case SerializedPropertyType.Integer:
46+
EditorGUI.IntSlider(sliderBox, property, (int) attr.Min, (int) attr.Max, label);
47+
break;
48+
case SerializedPropertyType.Vector2:
49+
case SerializedPropertyType.Vector2Int:
50+
MinMaxSlider(sliderBox, property, attr.Min, attr.Max, label);
51+
break;
52+
default:
53+
EditorGUI.LabelField(position, label, new GUIContent("Slider must be of type float, int, Vector2, or Vector2Int"));
54+
return;
55+
}
56+
57+
}
58+
59+
private static void MinMaxSlider(Rect position, SerializedProperty property, float min, float max, GUIContent label)
60+
{
61+
float minValue = 0;
62+
float maxValue = 0;
63+
64+
if (property.propertyType == SerializedPropertyType.Vector2)
65+
{
66+
minValue = property.vector2Value.x;
67+
maxValue = property.vector2Value.y;
68+
EditorGUI.MinMaxSlider(position, label, ref minValue, ref maxValue, min, max);
69+
property.vector2Value = new Vector2(minValue, maxValue);
70+
}
71+
else if (property.propertyType == SerializedPropertyType.Vector2Int)
72+
{
73+
minValue = property.vector2IntValue.x;
74+
maxValue = property.vector2IntValue.y;
75+
EditorGUI.MinMaxSlider(position, label, ref minValue, ref maxValue, min, max);
76+
property.vector2IntValue = new Vector2Int(Mathf.RoundToInt(minValue), Mathf.RoundToInt(maxValue));
77+
}
78+
else
79+
{
80+
throw new System.ArgumentException("The property must be a vector2 type");
81+
}
82+
}
83+
84+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
85+
{
86+
float height = base.GetPropertyHeight(property, label);
87+
88+
SlideAttribute attr = attribute as SlideAttribute;
89+
if (attr != null && (attr.LeftLabel != null || attr.RightLabel != null))
90+
height += -miniLabelVerticalOffset;
91+
92+
return height;
93+
}
94+
}
95+
}

Editor/Attributes/SlideDrawer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Attributes/SlideAttribute.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using UnityEngine;
7+
8+
namespace Lachee.Attributes
9+
{
10+
[System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple = false)]
11+
public sealed class SlideAttribute : PropertyAttribute
12+
{
13+
public float Min { get; }
14+
public float Max { get; }
15+
16+
public GUIContent LeftLabel { get; }
17+
public GUIContent RightLabel { get; }
18+
19+
public SlideAttribute(float min, float max)
20+
{
21+
this.Min = min;
22+
this.Max = max;
23+
LeftLabel = null;
24+
RightLabel = null;
25+
}
26+
27+
public SlideAttribute(float min, float max, string leftLabel, string rightLabel)
28+
: this(min, max, new GUIContent(leftLabel), new GUIContent(rightLabel)) { }
29+
30+
public SlideAttribute(float min, float max, GUIContent leftLabel, GUIContent rightLabel)
31+
{
32+
this.Min = min;
33+
this.Max = max;
34+
this.LeftLabel = leftLabel;
35+
this.RightLabel = rightLabel;
36+
}
37+
}
38+
}

Runtime/Attributes/SlideAttribute.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.lachee.utilities",
3-
"version": "1.5.0",
3+
"version": "1.5.1",
44
"displayName": "Lachee's Utilities",
55
"description": "Bunch of utility functionality",
66
"unity": "2019.1",

0 commit comments

Comments
 (0)