Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MinMaxSlider
using UnityEngine;
using System.Collections;
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;
// based on: https://gist.github.com/frarees/9791517
[CustomPropertyDrawer(typeof(MinMaxSliderAttribute))]
class MinMaxSliderDrawer : PropertyDrawer
{
/// <summary>インデント段階を保存</summary>
private int lastIndentLevel;
/// <summary>インデント量</summary>
private int indentAmount;
/// <summary>描画領域全体</summary>
private Rect wholeRect;
/// <summary>ラベルの幅</summary>
private const float LabelWidth = 60;
/// <summary>ラベルの間隔</summary>
private const float LabelMargin = 5;
/// <summary>
/// 描画を行う
/// </summary>
/// <param name="position">描画範囲</param>
/// <param name="property">対象プロパティ</param>
/// <param name="label">表示するラベル</param>
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
BeginProperty(position, property, label);
// Vector2のみ対応
if (property.propertyType == SerializedPropertyType.Vector2)
{
// 値の取得
Vector2 range = property.vector2Value;
float min = range.x;
float max = range.y;
MinMaxSliderAttribute attr = attribute as MinMaxSliderAttribute;
EditorGUI.BeginChangeCheck();
var rect = this.wholeRect;
// min field
rect.width = LabelWidth;
min = EditorGUI.FloatField(rect, min);
min = Mathf.Clamp(min, attr.min, max);
// min max slider
rect.x += LabelWidth + LabelMargin;
rect.width = this.wholeRect.width - LabelWidth * 2 - LabelMargin * 2;
EditorGUI.MinMaxSlider(new GUIContent(), rect, ref min, ref max, attr.min, attr.max);
// max field
rect.x += this.wholeRect.width - LabelWidth * 2 - LabelMargin;
rect.width = LabelWidth;
max = EditorGUI.FloatField(rect, max);
max = Mathf.Clamp(max, min, attr.max);
// 変更の適用
if (EditorGUI.EndChangeCheck())
{
range.x = min;
range.y = max;
property.vector2Value = range;
}
}
else
{
EditorGUI.LabelField(position, label, "Use only with Vector2");
}
EndProperty();
}
/// <summary>
/// プロパティの開始
/// </summary>
/// <param name="position">OnGUIと同じ</param>
/// <param name="property">OnGUIと同じ</param>
/// <param name="label">OnGUIと同じ</param>
protected void BeginProperty(Rect position, SerializedProperty property, GUIContent label)
{
this.indentAmount = EditorGUI.indentLevel * 16;
EditorGUIUtility.labelWidth = EditorGUIUtility.labelWidth;
label = EditorGUI.BeginProperty(position, label, property);
this.lastIndentLevel = EditorGUI.indentLevel;
this.wholeRect = EditorGUI.PrefixLabel(position, label);
EditorGUI.indentLevel = 0;
}
/// <summary>
/// プロパティの終了
/// </summary>
protected void EndProperty()
{
EditorGUI.indentLevel = this.lastIndentLevel;
EditorGUI.EndProperty();
}
}
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
[MinMaxSlider(0, 255)]
public Vector2 range;
}
@TwisterK

This comment has been minimized.

Copy link

@TwisterK TwisterK commented Jul 28, 2016

Awesome script, thanks for sharing.

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