-
-
Save srndpty/212be8a19ec9dc277f421243dd1eddfd to your computer and use it in GitHub Desktop.
MinMaxSlider
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
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; | |
} | |
} |
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
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(); | |
} | |
} |
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
using UnityEngine; | |
using System.Collections; | |
public class Test : MonoBehaviour | |
{ | |
[MinMaxSlider(0, 255)] | |
public Vector2 range; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome script, thanks for sharing.