Skip to content

Instantly share code, notes, and snippets.

@Vilyx
Created April 6, 2018 12:10
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Vilyx/8ffe17f943165b661d80fdb498ab3088 to your computer and use it in GitHub Desktop.
Save Vilyx/8ffe17f943165b661d80fdb498ab3088 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
[AttributeUsage(AttributeTargets.Field)]
public class MinMaxSliderAttribute : PropertyAttribute
{
public readonly float max;
public readonly float min;
public MinMaxSliderAttribute(float min, float max)
{
this.min = min;
this.max = max;
}
}
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(MinMaxSliderAttribute))]
class MinMaxSliderDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property.propertyType == SerializedPropertyType.Vector2)
{
float textFieldWidth = 30;
EditorGUI.LabelField(position, label);
Vector2 range = property.vector2Value;
float min = range.x;
float max = range.y;
MinMaxSliderAttribute attr = attribute as MinMaxSliderAttribute;
Rect sliderPos = position;
sliderPos.x += EditorGUIUtility.labelWidth + textFieldWidth;
sliderPos.width -= EditorGUIUtility.labelWidth + textFieldWidth*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;
}
EditorGUI.LabelField(position, "");
Rect minPos = position;
minPos.x += EditorGUIUtility.labelWidth;
minPos.width = textFieldWidth;
EditorGUI.LabelField(minPos, min.ToString("0.00"));
Rect maxPos = position;
maxPos.x += maxPos.width - textFieldWidth;
maxPos.width = textFieldWidth;
EditorGUI.LabelField(maxPos, max.ToString("0.00"));
}
else
{
EditorGUI.LabelField(position, label, "Use only with Vector2");
}
}
}
@listopad
Copy link

Nice editor improvement. Thank you!

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