Skip to content

Instantly share code, notes, and snippets.

@AShim3D
Forked from frarees/MinMaxSliderAttribute.cs
Last active August 29, 2015 14:25

Revisions

  1. AShim3D revised this gist Jul 20, 2015. 2 changed files with 43 additions and 27 deletions.
    18 changes: 10 additions & 8 deletions MinMaxSliderAttribute.cs
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,15 @@
    using System;
    using UnityEngine;

    public class MinMaxSliderAttribute : PropertyAttribute {

    public readonly float max;
    public readonly float min;
    public readonly float max;
    public readonly float min;
    public readonly bool showFLoatFields;

    public MinMaxSliderAttribute (float min, float max) {
    this.min = min;
    this.max = max;
    }
    }
    public MinMaxSliderAttribute(float min, float max, bool showFLoatFields = false) {
    this.min = min;
    this.max = max;
    this.showFLoatFields = showFLoatFields;
    }

    }
    52 changes: 33 additions & 19 deletions MinMaxSliderDrawer.cs
    Original file line number Diff line number Diff line change
    @@ -3,23 +3,37 @@

    [CustomPropertyDrawer (typeof (MinMaxSliderAttribute))]
    class MinMaxSliderDrawer : PropertyDrawer {

    public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {

    if (property.propertyType == SerializedPropertyType.Vector2) {
    Vector2 range = property.vector2Value;
    float min = range.x;
    float max = range.y;
    MinMaxSliderAttribute attr = attribute as MinMaxSliderAttribute;
    EditorGUI.BeginChangeCheck ();
    EditorGUI.MinMaxSlider (label, position, ref min, ref max, attr.min, attr.max);
    if (EditorGUI.EndChangeCheck ()) {
    range.x = min;
    range.y = max;
    property.vector2Value = range;
    }
    } else {
    EditorGUI.LabelField (position, label, "Use only with Vector2");
    }
    }
    }
    private const float FIELD_WIDTH = 32f;
    private const float SPACE_WIDTH = 4f;

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
    if (property.propertyType == SerializedPropertyType.Vector2) {
    Vector2 range = property.vector2Value;
    float min = range.x;
    float max = range.y;
    var attr = attribute as MinMaxSliderAttribute;
    EditorGUI.BeginChangeCheck();
    position = EditorGUI.PrefixLabel(position, -1, label);
    var sliderPosition = position;
    if (attr.showFLoatFields) {
    sliderPosition.x += FIELD_WIDTH + SPACE_WIDTH;
    sliderPosition.width -= 2f * (FIELD_WIDTH + SPACE_WIDTH);
    var minPosition = position;
    minPosition.width = FIELD_WIDTH;
    min = EditorGUI.FloatField(minPosition, min);
    var maxPosition = minPosition;
    maxPosition.x += position.width - FIELD_WIDTH;
    max = EditorGUI.FloatField(maxPosition, max);
    }
    EditorGUI.MinMaxSlider(sliderPosition, ref min, ref max, attr.min, attr.max);
    if (EditorGUI.EndChangeCheck()) {
    range.x = min;
    range.y = max;
    property.vector2Value = range;
    }
    } else {
    EditorGUI.LabelField(position, label, "Use only with Vector2");
    }
    }
    }
  2. @frarees frarees created this gist Mar 26, 2014.
    13 changes: 13 additions & 0 deletions MinMaxSliderAttribute.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    using System;
    using UnityEngine;

    public class MinMaxSliderAttribute : PropertyAttribute {

    public readonly float max;
    public readonly float min;

    public MinMaxSliderAttribute (float min, float max) {
    this.min = min;
    this.max = max;
    }
    }
    25 changes: 25 additions & 0 deletions MinMaxSliderDrawer.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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) {
    Vector2 range = property.vector2Value;
    float min = range.x;
    float max = range.y;
    MinMaxSliderAttribute attr = attribute as MinMaxSliderAttribute;
    EditorGUI.BeginChangeCheck ();
    EditorGUI.MinMaxSlider (label, position, ref min, ref max, attr.min, attr.max);
    if (EditorGUI.EndChangeCheck ()) {
    range.x = min;
    range.y = max;
    property.vector2Value = range;
    }
    } else {
    EditorGUI.LabelField (position, label, "Use only with Vector2");
    }
    }
    }