MinMaxPropertyDrawer.cs 2.2 KB

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