| 1 | using UnityEditor; |
| 2 | using UnityEngine; |
| 3 | |
| 4 | namespace OVR |
| 5 | { |
| 6 | |
| 7 | /* |
| 8 | ----------------------- |
| 9 | |
| 10 | MinMaxPropertyDrawer |
| 11 | |
| 12 | ----------------------- |
| 13 | */ |
| 14 | [CustomPropertyDrawer (typeof (MinMaxAttribute))] |
| 15 | public class MinMaxPropertyDrawer : PropertyDrawer { |
| 16 | |
| 17 | // Provide easy access to the MinMaxAttribute for reading information from it. |
| 18 | MinMaxAttribute minMax { get { return ((MinMaxAttribute)attribute); } } |
| 19 | |
| 20 | /* |
| 21 | ----------------------- |
| 22 | GetPropertyHeight() |
| 23 | ----------------------- |
| 24 | */ |
| 25 | public override float GetPropertyHeight( SerializedProperty prop, GUIContent label ) { |
| 26 | return base.GetPropertyHeight( prop, label ) * 2f; |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | ----------------------- |
| 31 | OnGUI() |
| 32 | ----------------------- |
| 33 | */ |
| 34 | public override void OnGUI( Rect position, SerializedProperty property, GUIContent label ) { |
| 35 | Rect sliderPosition = EditorGUI.PrefixLabel( position, label ); |
| 36 | SerializedProperty min = property.FindPropertyRelative( "x" ); |
| 37 | SerializedProperty max = property.FindPropertyRelative( "y" ); |
| 38 | |
| 39 | // draw the range and the reset button first so that the slider doesn't grab all the input |
| 40 | Rect rangePosition = sliderPosition; |
| 41 | rangePosition.y += rangePosition.height * 0.5f; |
| 42 | rangePosition.height *= 0.5f; |
| 43 | Rect contentPosition = rangePosition; |
| 44 | EditorGUI.indentLevel = 0; |
| 45 | EditorGUIUtility.labelWidth = 30f; |
| 46 | contentPosition.width *= 0.3f; |
| 47 | EditorGUI.PropertyField(contentPosition, min, new GUIContent( "Min" ) ); |
| 48 | contentPosition.x += contentPosition.width + 20f; |
| 49 | EditorGUI.PropertyField( contentPosition, max, new GUIContent( "Max" ) ); |
| 50 | contentPosition.x += contentPosition.width + 20f; |
| 51 | contentPosition.width = 50.0f; |
| 52 | if ( GUI.Button( contentPosition, "Reset" ) ) { |
| 53 | min.floatValue = minMax.minDefaultVal; |
| 54 | max.floatValue = minMax.maxDefaultVal; |
| 55 | } |
| 56 | float minValue = min.floatValue; |
| 57 | float maxValue = max.floatValue; |
| 58 | #if UNITY_2017_1_OR_NEWER |
| 59 | EditorGUI.MinMaxSlider( sliderPosition, GUIContent.none, ref minValue, ref maxValue, minMax.min, minMax.max ); |
| 60 | #else |
| 61 | EditorGUI.MinMaxSlider( GUIContent.none, sliderPosition, ref minValue, ref maxValue, minMax.min, minMax.max ); |
| 62 | #endif |
| 63 | // round to readable values |
| 64 | min.floatValue = Mathf.Round( minValue / 0.01f ) * 0.01f; |
| 65 | max.floatValue = Mathf.Round( maxValue / 0.01f ) * 0.01f; |
| 66 | } |
| 67 | |
| 68 | } |
| 69 | |
| 70 | } // namespace OVR |