-
-
Save hitarthdoc/b622f3b4d85f160f9e3e5101489c1e6d 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.
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
// 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; | |
} | |
} |
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
// 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 and vector2Int! 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; | |
float [] vals = new float [] { minLimit, minValue, maxValue, maxLimit }; // shove the values and limits into a vector4 and draw them all at once | |
EditorGUI.MultiFloatField ( position, new GUIContent ( "Range" ), new GUIContent [] { new GUIContent ( "MinLimit" ), new GUIContent ( "MinVal" ), new GUIContent ( "MaxVal" ), new GUIContent ( "MaxLimit" ) }, vals ); | |
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 ( vals [ 1 ], vals [ 2 ] ); // save off any change to the value~ | |
} | |
} | |
} | |
else if ( property.propertyType == SerializedPropertyType.Vector2Int ) | |
{ | |
// 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.vector2IntValue.x; // the currently set minimum and maximum value | |
float maxValue = property.vector2IntValue.y; | |
int minLimit = Mathf.RoundToInt ( minMax.MinLimit ); // the limit for both min and max, min cant go lower than minLimit and maax cant top maxLimit | |
int maxLimit = Mathf.RoundToInt ( minMax.MaxLimit ); | |
// and ask unity to draw them all nice for us! | |
EditorGUI.MinMaxSlider ( position, label, ref minValue, ref maxValue, minLimit, maxLimit ); | |
var vec = Vector2Int.zero; // save the results into the property! | |
vec.x = Mathf.RoundToInt ( minValue ); | |
vec.y = Mathf.RoundToInt ( maxValue ); | |
property.vector2IntValue = 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; | |
float [] vals = new float [] { minLimit, minValue, maxValue, maxLimit }; // shove the values and limits into a vector4 and draw them all at once | |
EditorGUI.MultiFloatField ( position, new GUIContent ( "Range" ), new GUIContent [] { new GUIContent ( "MinLimit" ), new GUIContent ( "MinVal" ), new GUIContent ( "MaxVal" ), new GUIContent ( "MaxLimit" ) }, vals ); | |
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.vector2IntValue = new Vector2Int ( Mathf.RoundToInt ( vals [ 1 ] ), Mathf.RoundToInt ( vals [ 2 ] ) ); // 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 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
// 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 () { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Minor formatting changes in MinMaxDrawer in Revision 6.