Created
September 6, 2017 14:32
-
-
Save unity3dcollege/ecd6a806fd438679696372929c34cf4c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
[CustomPropertyDrawer(typeof(RangedFloat))] | |
public class RangedFloatDrawer : PropertyDrawer | |
{ | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
label = EditorGUI.BeginProperty(position, label, property); | |
position = EditorGUI.PrefixLabel(position, label); | |
SerializedProperty minProp = property.FindPropertyRelative("minValue"); | |
SerializedProperty maxProp = property.FindPropertyRelative("maxValue"); | |
float minValue = minProp.floatValue; | |
float maxValue = maxProp.floatValue; | |
float rangeMin = 0; | |
float rangeMax = 1; | |
var ranges = (MinMaxRangeAttribute[])fieldInfo.GetCustomAttributes(typeof(MinMaxRangeAttribute), true); | |
if (ranges.Length > 0) | |
{ | |
rangeMin = ranges[0].Min; | |
rangeMax = ranges[0].Max; | |
} | |
const float rangeBoundsLabelWidth = 40f; | |
var rangeBoundsLabel1Rect = new Rect(position); | |
rangeBoundsLabel1Rect.width = rangeBoundsLabelWidth; | |
GUI.Label(rangeBoundsLabel1Rect, new GUIContent(minValue.ToString("F2"))); | |
position.xMin += rangeBoundsLabelWidth; | |
var rangeBoundsLabel2Rect = new Rect(position); | |
rangeBoundsLabel2Rect.xMin = rangeBoundsLabel2Rect.xMax - rangeBoundsLabelWidth; | |
GUI.Label(rangeBoundsLabel2Rect, new GUIContent(maxValue.ToString("F2"))); | |
position.xMax -= rangeBoundsLabelWidth; | |
EditorGUI.BeginChangeCheck(); | |
EditorGUI.MinMaxSlider(position, ref minValue, ref maxValue, rangeMin, rangeMax); | |
if (EditorGUI.EndChangeCheck()) | |
{ | |
minProp.floatValue = minValue; | |
maxProp.floatValue = maxValue; | |
} | |
EditorGUI.EndProperty(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment