Tanoda
RangeSliderEditor.cs
Go to the documentation of this file.
1
5
6using UnityEditor;
7using UnityEditor.UI;
8
10{
11 [CustomEditor(typeof(RangeSlider), true)]
12 [CanEditMultipleObjects]
13 public class RangeSliderEditor : SelectableEditor
14 {
15 SerializedProperty m_LowHandleRect;
16 SerializedProperty m_HighHandleRect;
17 SerializedProperty m_FillRect;
18
19 SerializedProperty m_MinValue;
20 SerializedProperty m_MaxValue;
21 SerializedProperty m_WholeNumbers;
22
23 SerializedProperty m_LowValue;
24 SerializedProperty m_HighValue;
25
26 //need ref values for the editor MinMaxSlider
27 float low = 0;
28 float high = 1;
29
30 SerializedProperty m_OnValueChanged;
31
32
33 protected override void OnEnable()
34 {
35 base.OnEnable();
36 m_LowHandleRect = serializedObject.FindProperty("m_LowHandleRect");
37 m_HighHandleRect = serializedObject.FindProperty("m_HighHandleRect");
38 m_FillRect = serializedObject.FindProperty("m_FillRect");
39
40 m_MinValue = serializedObject.FindProperty("m_MinValue");
41 m_MaxValue = serializedObject.FindProperty("m_MaxValue");
42 m_WholeNumbers = serializedObject.FindProperty("m_WholeNumbers");
43
44 m_LowValue = serializedObject.FindProperty("m_LowValue");
45 low = m_LowValue.floatValue;
46 m_HighValue = serializedObject.FindProperty("m_HighValue");
47 high = m_HighValue.floatValue;
48
49 m_OnValueChanged = serializedObject.FindProperty("m_OnValueChanged");
50 }
51
52 public override void OnInspectorGUI()
53 {
54 base.OnInspectorGUI();
55 EditorGUILayout.Space();
56
57 serializedObject.Update();
58 //grab the updated value affected by m_WholeNumbers
59 low = m_LowValue.floatValue;
60 high = m_HighValue.floatValue;
61
62 EditorGUILayout.PropertyField(m_LowHandleRect);
63 EditorGUILayout.PropertyField(m_HighHandleRect);
64 EditorGUILayout.PropertyField(m_FillRect);
65
66 if (m_LowHandleRect.objectReferenceValue != null && m_HighHandleRect.objectReferenceValue != null)
67 {
68 EditorGUI.BeginChangeCheck();
69
70 EditorGUILayout.PropertyField(m_MinValue);
71 EditorGUILayout.PropertyField(m_MaxValue);
72 EditorGUILayout.PropertyField(m_WholeNumbers);
73
74 //We're going to do a fair bit of layout here
75 EditorGUILayout.BeginHorizontal();
76 //Low Label and value
77 EditorGUILayout.BeginVertical();
78 EditorGUILayout.BeginHorizontal();
79 GUILayout.FlexibleSpace();
80 GUILayout.Label("Low");
81 GUILayout.FlexibleSpace();
82 EditorGUILayout.EndHorizontal();
83 low = EditorGUILayout.DelayedFloatField(low, GUILayout.MaxWidth(100));
84 EditorGUILayout.EndVertical();
85
86 GUILayout.FlexibleSpace();
87
88 //Slider
89 EditorGUILayout.BeginVertical();
90 GUILayout.FlexibleSpace();
91 EditorGUILayout.MinMaxSlider(ref low, ref high, m_MinValue.floatValue, m_MaxValue.floatValue, GUILayout.ExpandWidth(true));
92 EditorGUILayout.EndVertical();
93
94 GUILayout.FlexibleSpace();
95
96 //High label and value
97 EditorGUILayout.BeginVertical();
98 EditorGUILayout.BeginHorizontal();
99 GUILayout.FlexibleSpace();
100 GUILayout.Label("High");
101 GUILayout.FlexibleSpace();
102 EditorGUILayout.EndHorizontal();
103 high = EditorGUILayout.DelayedFloatField(high, GUILayout.MaxWidth(100));
104 EditorGUILayout.EndVertical();
105 EditorGUILayout.EndHorizontal();
106
107 m_LowValue.floatValue = low;
108 m_HighValue.floatValue = high;
109
110 EditorGUILayout.Space();
111 EditorGUILayout.PropertyField(m_OnValueChanged);
112 }
113 else
114 {
115 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);
116 }
117
118 serializedObject.ApplyModifiedProperties();
119 }
120 }
121
122}
123
Credit Erdener Gonenc - @PixelEnvision.