Skip to content

Instantly share code, notes, and snippets.

@LotteMakesStuff
Last active March 16, 2023 16:47
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save LotteMakesStuff/0de9be35044bab97cbe79b9ced695585 to your computer and use it in GitHub Desktop.
Save LotteMakesStuff/0de9be35044bab97cbe79b9ced695585 to your computer and use it in GitHub Desktop.
MinMax property drawer for Unity - Add a [MinMax] attribute to a property to draw a useful min/max setting slider.
// NOTE DONT put in an editor folder
using UnityEngine;
public class MinMaxAttribute : PropertyAttribute
{
public float MinLimit = 0;
public float MaxLimit = 1;
public bool ShowEditRange;
public bool ShowDebugValues;
public MinMaxAttribute(int min, int max)
{
MinLimit = min;
MaxLimit = max;
}
}
// NOTE put in a Editor folder
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(MinMaxAttribute))]
public class MinMaxDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// cast the attribute to make life easier
MinMaxAttribute minMax = attribute as MinMaxAttribute;
// This only works on a vector2! 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!
EditorGUI.MinMaxSlider(position, label, ref minValue, ref maxValue, minLimit, maxLimit);
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 = false;
if (minMax.ShowEditRange)
{
isEditable = true;
}
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;
Vector4 val = new Vector4(minLimit, minValue, maxValue, maxLimit); // shove the values and limits into a vector4 and draw them all at once
val = EditorGUI.Vector4Field(position, "MinLimit/MinVal/MaxVal/MaxLimit", val);
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(val.y,val.z); // save off any change to the value~
}
}
}
}
// this method lets unity know how big to draw the property. We need to override this because it could end up meing more than one line big
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
MinMaxAttribute minMax = attribute as MinMaxAttribute;
// by default just return the standard line height
float size = EditorGUIUtility.singleLineHeight;
// if we have a special mode, add two extra lines!
if (minMax.ShowEditRange || minMax.ShowDebugValues)
{
size += EditorGUIUtility.singleLineHeight*2;
}
return size;
}
}
// This componnet Demos how to use MinMax attribute! Uses the ReadOnly attribute found here https://gist.github.com/LotteMakesStuff/c0a3b404524be57574ffa5f8270268ea
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class VolumeRandomizer : MonoBehaviour {
public AudioSource source;
public float Size;
[MinMax(0,1, ShowEditRange = true)]
public Vector2 VolumeRange = new Vector2(0, 1);
// Use this for initialization
void Start () {
}
// Update is called once per frame
[ExecuteInEditMode]
void Update () {
}
}
@hitarthdoc
Copy link

Hi @LotteMakesStuff,
Can you add my changes about adding Vector2Int support for the drawer.
My fork and changes
Regards,
hitarthdoc

@Erikoinen
Copy link

I'm 99% sure I've implemented this properly, but for some reason none of the handlers become visible when using the attribute. Any idea why?

@hitarthdoc
Copy link

Hi @LotteMakesStuff,
Made more changes made in My fork and changes.
Removed EditorGUI Vector4 field and used EditorGUI MultiFloatField along with support for Vector2Int.

@Cxyda
Copy link

Cxyda commented Sep 5, 2022

I'm 99% sure I've implemented this properly, but for some reason none of the handlers become visible when using the attribute. Any idea why?

Sorry for necroing this, but hat the same issue... turned out the attribute only works on Vector2 fields, which is kinda obvious when looking at the code ^^

Thanks for sharing this

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