RangeSliderEditor.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /// Credit Ben MacKinnon @Dover8
  2. /// Sourced from - https://github.com/Dover8/Unity-UI-Extensions/tree/range-slider
  3. /// Usage: Extension of the standard slider. Two handles determine a low and high value between a Min and Max.
  4. /// Raises a UnityEvent passing the low and high values
  5. using UnityEditor;
  6. using UnityEditor.UI;
  7. namespace UnityEngine.UI.Extensions
  8. {
  9. [CustomEditor(typeof(RangeSlider), true)]
  10. [CanEditMultipleObjects]
  11. public class RangeSliderEditor : SelectableEditor
  12. {
  13. SerializedProperty m_LowHandleRect;
  14. SerializedProperty m_HighHandleRect;
  15. SerializedProperty m_FillRect;
  16. SerializedProperty m_MinValue;
  17. SerializedProperty m_MaxValue;
  18. SerializedProperty m_WholeNumbers;
  19. SerializedProperty m_LowValue;
  20. SerializedProperty m_HighValue;
  21. //need ref values for the editor MinMaxSlider
  22. float low = 0;
  23. float high = 1;
  24. SerializedProperty m_OnValueChanged;
  25. protected override void OnEnable()
  26. {
  27. base.OnEnable();
  28. m_LowHandleRect = serializedObject.FindProperty("m_LowHandleRect");
  29. m_HighHandleRect = serializedObject.FindProperty("m_HighHandleRect");
  30. m_FillRect = serializedObject.FindProperty("m_FillRect");
  31. m_MinValue = serializedObject.FindProperty("m_MinValue");
  32. m_MaxValue = serializedObject.FindProperty("m_MaxValue");
  33. m_WholeNumbers = serializedObject.FindProperty("m_WholeNumbers");
  34. m_LowValue = serializedObject.FindProperty("m_LowValue");
  35. low = m_LowValue.floatValue;
  36. m_HighValue = serializedObject.FindProperty("m_HighValue");
  37. high = m_HighValue.floatValue;
  38. m_OnValueChanged = serializedObject.FindProperty("m_OnValueChanged");
  39. }
  40. public override void OnInspectorGUI()
  41. {
  42. base.OnInspectorGUI();
  43. EditorGUILayout.Space();
  44. serializedObject.Update();
  45. //grab the updated value affected by m_WholeNumbers
  46. low = m_LowValue.floatValue;
  47. high = m_HighValue.floatValue;
  48. EditorGUILayout.PropertyField(m_LowHandleRect);
  49. EditorGUILayout.PropertyField(m_HighHandleRect);
  50. EditorGUILayout.PropertyField(m_FillRect);
  51. if (m_LowHandleRect.objectReferenceValue != null && m_HighHandleRect.objectReferenceValue != null)
  52. {
  53. EditorGUI.BeginChangeCheck();
  54. EditorGUILayout.PropertyField(m_MinValue);
  55. EditorGUILayout.PropertyField(m_MaxValue);
  56. EditorGUILayout.PropertyField(m_WholeNumbers);
  57. //We're going to do a fair bit of layout here
  58. EditorGUILayout.BeginHorizontal();
  59. //Low Label and value
  60. EditorGUILayout.BeginVertical();
  61. EditorGUILayout.BeginHorizontal();
  62. GUILayout.FlexibleSpace();
  63. GUILayout.Label("Low");
  64. GUILayout.FlexibleSpace();
  65. EditorGUILayout.EndHorizontal();
  66. low = EditorGUILayout.DelayedFloatField(low, GUILayout.MaxWidth(100));
  67. EditorGUILayout.EndVertical();
  68. GUILayout.FlexibleSpace();
  69. //Slider
  70. EditorGUILayout.BeginVertical();
  71. GUILayout.FlexibleSpace();
  72. EditorGUILayout.MinMaxSlider(ref low, ref high, m_MinValue.floatValue, m_MaxValue.floatValue, GUILayout.ExpandWidth(true));
  73. EditorGUILayout.EndVertical();
  74. GUILayout.FlexibleSpace();
  75. //High label and value
  76. EditorGUILayout.BeginVertical();
  77. EditorGUILayout.BeginHorizontal();
  78. GUILayout.FlexibleSpace();
  79. GUILayout.Label("High");
  80. GUILayout.FlexibleSpace();
  81. EditorGUILayout.EndHorizontal();
  82. high = EditorGUILayout.DelayedFloatField(high, GUILayout.MaxWidth(100));
  83. EditorGUILayout.EndVertical();
  84. EditorGUILayout.EndHorizontal();
  85. m_LowValue.floatValue = low;
  86. m_HighValue.floatValue = high;
  87. EditorGUILayout.Space();
  88. EditorGUILayout.PropertyField(m_OnValueChanged);
  89. }
  90. else
  91. {
  92. EditorGUILayout.HelpBox("Specify a RectTransform for the RangeSlider fill or the RangeSlider handles or both. Each must have a parent RectTransform that it can slide within.", MessageType.Info);
  93. }
  94. serializedObject.ApplyModifiedProperties();
  95. }
  96. }
  97. }