Tanoda
ScrollerEditor.cs
Go to the documentation of this file.
1
3
4using UnityEditor;
5using UnityEditor.AnimatedValues;
6
7// For maintenance, every new [SerializeField] variable in Scroller must be declared here
8
10{
11 [CustomEditor(typeof(Scroller))]
12 [CanEditMultipleObjects]
13 public class ScrollerEditor : Editor
14 {
15 SerializedProperty viewport;
16 SerializedProperty scrollDirection;
17 SerializedProperty movementType;
18 SerializedProperty elasticity;
19 SerializedProperty scrollSensitivity;
20 SerializedProperty inertia;
21 SerializedProperty decelerationRate;
22 SerializedProperty snap;
23 SerializedProperty snapEnable;
24 SerializedProperty snapVelocityThreshold;
25 SerializedProperty snapDuration;
26 SerializedProperty snapEasing;
27 SerializedProperty draggable;
28 SerializedProperty scrollbar;
29
30 AnimBool showElasticity;
31 AnimBool showInertiaRelatedValues;
32 AnimBool showSnapEnableRelatedValues;
33
34 void OnEnable()
35 {
36 viewport = serializedObject.FindProperty("viewport");
37 scrollDirection = serializedObject.FindProperty("scrollDirection");
38 movementType = serializedObject.FindProperty("movementType");
39 elasticity = serializedObject.FindProperty("elasticity");
40 scrollSensitivity = serializedObject.FindProperty("scrollSensitivity");
41 inertia = serializedObject.FindProperty("inertia");
42 decelerationRate = serializedObject.FindProperty("decelerationRate");
43 snap = serializedObject.FindProperty("snap");
44 snapEnable = serializedObject.FindProperty("snap.Enable");
45 snapVelocityThreshold = serializedObject.FindProperty("snap.VelocityThreshold");
46 snapDuration = serializedObject.FindProperty("snap.Duration");
47 snapEasing = serializedObject.FindProperty("snap.Easing");
48 draggable = serializedObject.FindProperty("draggable");
49 scrollbar = serializedObject.FindProperty("scrollbar");
50
51 showElasticity = new AnimBool(Repaint);
52 showInertiaRelatedValues = new AnimBool(Repaint);
53 showSnapEnableRelatedValues = new AnimBool(Repaint);
54 SetAnimBools(true);
55 }
56
57 void OnDisable()
58 {
59 showElasticity.valueChanged.RemoveListener(Repaint);
60 showInertiaRelatedValues.valueChanged.RemoveListener(Repaint);
61 showSnapEnableRelatedValues.valueChanged.RemoveListener(Repaint);
62 }
63
64 void SetAnimBools(bool instant)
65 {
66 SetAnimBool(showElasticity, !movementType.hasMultipleDifferentValues && movementType.enumValueIndex == (int)MovementType.Elastic, instant);
67 SetAnimBool(showInertiaRelatedValues, !inertia.hasMultipleDifferentValues && inertia.boolValue, instant);
68 SetAnimBool(showSnapEnableRelatedValues, !snapEnable.hasMultipleDifferentValues && snapEnable.boolValue, instant);
69 }
70
71 void SetAnimBool(AnimBool a, bool value, bool instant)
72 {
73 if (instant)
74 {
75 a.value = value;
76 }
77 else
78 {
79 a.target = value;
80 }
81 }
82
83 public override void OnInspectorGUI()
84 {
85 SetAnimBools(false);
86
87 serializedObject.Update();
88 EditorGUILayout.PropertyField(viewport);
89 EditorGUILayout.PropertyField(scrollDirection);
90 EditorGUILayout.PropertyField(movementType);
91 DrawMovementTypeRelatedValue();
92 EditorGUILayout.PropertyField(scrollSensitivity);
93 EditorGUILayout.PropertyField(inertia);
94 DrawInertiaRelatedValues();
95 EditorGUILayout.PropertyField(draggable);
96 EditorGUILayout.PropertyField(scrollbar);
97 serializedObject.ApplyModifiedProperties();
98 }
99
100 void DrawMovementTypeRelatedValue()
101 {
102 using (var group = new EditorGUILayout.FadeGroupScope(showElasticity.faded))
103 {
104 if (!group.visible)
105 {
106 return;
107 }
108
109 using (new EditorGUI.IndentLevelScope())
110 {
111 EditorGUILayout.PropertyField(elasticity);
112 }
113 }
114 }
115
116 void DrawInertiaRelatedValues()
117 {
118 using (var group = new EditorGUILayout.FadeGroupScope(showInertiaRelatedValues.faded))
119 {
120 if (!group.visible)
121 {
122 return;
123 }
124
125 using (new EditorGUI.IndentLevelScope())
126 {
127 EditorGUILayout.PropertyField(decelerationRate);
128 EditorGUILayout.PropertyField(snap);
129
130 using (new EditorGUI.IndentLevelScope())
131 {
132 DrawSnapRelatedValues();
133 }
134 }
135 }
136 }
137
138 void DrawSnapRelatedValues()
139 {
140 if (snap.isExpanded)
141 {
142 EditorGUILayout.PropertyField(snapEnable);
143
144 using (var group = new EditorGUILayout.FadeGroupScope(showSnapEnableRelatedValues.faded))
145 {
146 if (!group.visible)
147 {
148 return;
149 }
150
151 EditorGUILayout.PropertyField(snapVelocityThreshold);
152 EditorGUILayout.PropertyField(snapDuration);
153 EditorGUILayout.PropertyField(snapEasing);
154 }
155 }
156 }
157 }
158}
Credit Erdener Gonenc - @PixelEnvision.