-
-
Save FishOfTheNorthStar/e570ffe4084790dc76697ac3fb4a4f04 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
[AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)] | |
public class MinMaxSliderAttribute : PropertyAttribute | |
{ | |
public readonly float min; | |
public readonly float max; | |
public MinMaxSliderAttribute() : this(0, 1) {} | |
public MinMaxSliderAttribute(float min, float max) | |
{ | |
this.min = min; | |
this.max = max; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
[CustomPropertyDrawer(typeof(MinMaxSliderAttribute))] | |
class MinMaxSliderDrawer : PropertyDrawer | |
{ | |
const string kVectorMinName = "x"; | |
const string kVectorMaxName = "y"; | |
const float kFloatFieldWidth = 30f; | |
const float kSpacing = 2f; | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
if (property.propertyType == SerializedPropertyType.Vector2) | |
{ | |
float spacing = kSpacing * EditorGUIUtility.pixelsPerPoint; | |
float indentOffset = (float)EditorGUI.indentLevel * 15f; | |
Vector2 range = property.vector2Value; | |
float min = range.x; | |
float max = range.y; | |
float labelWidth = EditorGUIUtility.labelWidth; | |
if (EditorGUIUtility.hierarchyMode) | |
labelWidth -= indentOffset; | |
MinMaxSliderAttribute attr = attribute as MinMaxSliderAttribute; | |
EditorGUI.PrefixLabel(position, label); | |
Rect sliderPos = position; | |
sliderPos.x += labelWidth + kFloatFieldWidth + spacing; | |
sliderPos.width -= labelWidth + (kFloatFieldWidth + spacing) * 2; | |
EditorGUI.BeginChangeCheck(); | |
EditorGUI.MinMaxSlider(sliderPos, ref min, ref max, attr.min, attr.max); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
range.x = min; | |
range.y = max; | |
property.vector2Value = range; | |
} | |
Rect minPos = position; | |
minPos.x += labelWidth ; | |
minPos.width = kFloatFieldWidth + indentOffset; | |
EditorGUI.showMixedValue = property.FindPropertyRelative(kVectorMinName).hasMultipleDifferentValues; | |
EditorGUI.BeginChangeCheck(); | |
min = EditorGUI.FloatField(minPos, min); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
range.x = Mathf.Max(min, attr.min); | |
property.vector2Value = range; | |
} | |
Rect maxPos = position; | |
maxPos.x += maxPos.width - kFloatFieldWidth - indentOffset; | |
maxPos.width = kFloatFieldWidth + indentOffset; | |
EditorGUI.showMixedValue = property.FindPropertyRelative(kVectorMaxName).hasMultipleDifferentValues; | |
EditorGUI.BeginChangeCheck(); | |
max = EditorGUI.FloatField(maxPos, max); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
range.y = Mathf.Min(max, attr.max); | |
property.vector2Value = range; | |
} | |
EditorGUI.showMixedValue = false; | |
} | |
else | |
{ | |
EditorGUI.LabelField(position, label, new GUIContent("Vector2 support only")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Min/Max values were not displaying correctly when used within a foldout. This seems to correct that.