1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Collections.Generic;
namespace Lean.Common
{
/// <summary>This class allows you to quickly make custom inspectors with common features.</summary>
public class LeanInspector<T> : Editor
where T : Object
{
protected T Target;
protected T[] Targets;
private static readonly string[] propertyToExclude = new string[] { "m_Script" };
private static List<Color> colors = new List<Color>();
private static GUIContent customContent = new GUIContent();
public static void BeginError(bool error)
{
BeginError(error, Color.red);
}
public static void BeginError(bool error, Color color)
{
colors.Add(GUI.color);
GUI.color = error == true ? color : colors[0];
}
public static void EndError()
{
var index = colors.Count - 1;
GUI.color = colors[index];
colors.RemoveAt(index);
}
public static Rect Reserve()
{
var rect = EditorGUILayout.BeginVertical();
EditorGUILayout.LabelField(GUIContent.none);
EditorGUILayout.EndVertical();
return rect;
}
public override void OnInspectorGUI()
{
colors.Clear();
Target = (T)target;
Targets = targets.Select(t => (T)t).ToArray();
EditorGUI.BeginChangeCheck();
{
EditorGUILayout.Separator();
serializedObject.Update();
DrawInspector();
serializedObject.ApplyModifiedProperties();
EditorGUILayout.Separator();
}
if (EditorGUI.EndChangeCheck() == true)
{
GUI.changed = true; Repaint();
Dirty();
}
}
public virtual void OnSceneGUI()
{
Target = (T)target;
DrawScene();
}
protected void Each(System.Action<T> update, bool dirty = true)
{
if (dirty == true)
{
Undo.RecordObjects(Targets, "Inspector");
}
foreach (var t in Targets)
{
update(t);
}
if (dirty == true)
{
Dirty();
}
}
protected bool Any(System.Func<T, bool> check)
{
foreach (var t in Targets)
{
if (check(t) == true)
{
return true;
}
}
return false;
}
protected bool All(System.Func<T, bool> check)
{
foreach (var t in Targets)
{
if (check(t) == false)
{
return false;
}
}
return true;
}
protected bool DrawExpand(ref bool expand, string propertyPath, string overrideTooltip = null, string overrideText = null)
{
var rect = Reserve();
var property = serializedObject.FindProperty(propertyPath);
customContent.text = string.IsNullOrEmpty(overrideText ) == false ? overrideText : property.displayName;
customContent.tooltip = string.IsNullOrEmpty(overrideTooltip) == false ? overrideTooltip : property.tooltip;
EditorGUI.BeginChangeCheck();
EditorGUI.PropertyField(rect, property, customContent, true);
var changed = EditorGUI.EndChangeCheck();
expand = EditorGUI.Foldout(new Rect(rect.position, new Vector2(25.0f, rect.height)), expand, string.Empty);
return changed;
}
protected bool DrawMinMax(string propertyPath, float min, float max, string overrideTooltip = null, string overrideText = null)
{
var property = serializedObject.FindProperty(propertyPath);
var value = property.vector2Value;
customContent.text = string.IsNullOrEmpty(overrideText ) == false ? overrideText : property.displayName;
customContent.tooltip = string.IsNullOrEmpty(overrideTooltip) == false ? overrideTooltip : property.tooltip;
EditorGUI.BeginChangeCheck();
EditorGUILayout.MinMaxSlider(customContent, ref value.x, ref value.y, min, max);
if (EditorGUI.EndChangeCheck() == true)
{
property.vector2Value = value;
return true;
}
return false;
}
protected bool DrawEulerAngles(string propertyPath, string overrideTooltip = null, string overrideText = null)
{
var property = serializedObject.FindProperty(propertyPath);
var mixed = EditorGUI.showMixedValue;
customContent.text = string.IsNullOrEmpty(overrideText ) == false ? overrideText : property.displayName;
customContent.tooltip = string.IsNullOrEmpty(overrideTooltip) == false ? overrideTooltip : property.tooltip;
EditorGUI.BeginChangeCheck();
EditorGUI.showMixedValue = property.hasMultipleDifferentValues;
var oldEulerAngles = property.quaternionValue.eulerAngles;
var newEulerAngles = EditorGUILayout.Vector3Field(customContent, oldEulerAngles);
if (oldEulerAngles != newEulerAngles)
{
property.quaternionValue = Quaternion.Euler(newEulerAngles);
}
EditorGUI.showMixedValue = mixed;
return EditorGUI.EndChangeCheck();
}
protected bool Draw(string propertyPath, string overrideTooltip = null, string overrideText = null)
{
var property = serializedObject.FindProperty(propertyPath);
customContent.text = string.IsNullOrEmpty(overrideText ) == false ? overrideText : property.displayName;
customContent.tooltip = string.IsNullOrEmpty(overrideTooltip) == false ? overrideTooltip : property.tooltip;
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(property, customContent, true);
return EditorGUI.EndChangeCheck();
}
protected virtual void DrawInspector()
{
DrawPropertiesExcluding(serializedObject, propertyToExclude);
}
protected virtual void DrawScene()
{
}
protected void Dirty()
{
for (var i = targets.Length - 1; i >= 0; i--)
{
EditorUtility.SetDirty(targets[i]);
}
serializedObject.Update();
}
}
}
#endif
|