Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FishOfTheNorthStar/e570ffe4084790dc76697ac3fb4a4f04 to your computer and use it in GitHub Desktop.
Save FishOfTheNorthStar/e570ffe4084790dc76697ac3fb4a4f04 to your computer and use it in GitHub Desktop.
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;
}
}
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"));
}
}
}
@FishOfTheNorthStar
Copy link
Author

Min/Max values were not displaying correctly when used within a foldout. This seems to correct that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment