forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPresetEditor.cs
More file actions
253 lines (230 loc) · 9.29 KB
/
Copy pathPresetEditor.cs
File metadata and controls
253 lines (230 loc) · 9.29 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System.Collections.Generic;
using UnityEditorInternal;
using UnityEngine;
namespace UnityEditor.Presets
{
[CustomEditor(typeof(Preset))]
[CanEditMultipleObjects]
internal class PresetEditor : Editor
{
static class Style
{
public static GUIContent presetType = EditorGUIUtility.TrTextContent("Preset Type", "The Object type this Preset can be applied to.");
public static GUIStyle inspectorBig = new GUIStyle(EditorStyles.inspectorBig);
public static GUIStyle centerStyle = new GUIStyle() { alignment = TextAnchor.MiddleCenter };
static Style()
{
inspectorBig.padding.bottom -= 1;
}
}
class ReferenceCount
{
public int count;
public Object reference;
}
static Dictionary<int, ReferenceCount> s_References = new Dictionary<int, ReferenceCount>();
bool m_DisplayErrorPreset;
string m_SelectedPresetTypeName;
Dictionary<string, List<Object>> m_InspectedTypes = new Dictionary<string, List<Object>>();
Editor m_InternalEditor = null;
string m_NotSupportedEditorName = null;
void OnEnable()
{
m_InspectedTypes.Clear();
foreach (var o in targets)
{
var preset = (Preset)o;
string type = preset.GetTargetFullTypeName();
if (!m_InspectedTypes.ContainsKey(type))
{
m_InspectedTypes.Add(type, new List<Object>());
}
m_InspectedTypes[type].Add(o);
}
if (m_InspectedTypes.Count == 1)
{
var preset = (Preset)target;
if (preset.IsValid())
{
m_SelectedPresetTypeName = preset.GetTargetFullTypeName();
GenerateInternalEditor();
}
else
{
m_SelectedPresetTypeName = "Invalid";
m_DisplayErrorPreset = true;
}
}
}
void OnDisable()
{
DestroyInternalEditor();
}
public override void OnInspectorGUI()
{
if (m_DisplayErrorPreset)
{
EditorGUILayout.HelpBox("Unable to load this Preset, the type is not supported.", MessageType.Error);
}
else
{
DrawPresetData();
}
}
void DrawPresetData()
{
string presetType = m_InspectedTypes.Count > 1 ? EditorGUI.mixedValueContent.text : m_SelectedPresetTypeName;
var rect = EditorGUI.PrefixLabel(EditorGUILayout.GetControlRect(true), Style.presetType);
EditorGUI.SelectableLabel(rect, presetType);
if (m_InternalEditor != null)
{
DrawInternalInspector();
}
else if (!string.IsNullOrEmpty(m_NotSupportedEditorName))
{
DrawUnsupportedInspector();
}
}
internal override void OnHeaderControlsGUI()
{
using (new EditorGUI.DisabledScope(targets.Length != 1 || Preset.IsPresetExcludedFromDefaultPresets(target as Preset)))
{
var preset = (Preset)target;
if (Preset.GetDefaultForPreset(preset) == preset)
{
if (GUILayout.Button(string.Format("Remove from {0} Default", preset.GetTargetTypeName()), EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
{
Preset.RemoveFromDefault(preset);
}
}
else
{
if (GUILayout.Button(string.Format("Set as {0} Default", preset.GetTargetTypeName()), EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
{
Preset.SetAsDefault(preset);
}
}
}
}
internal override void OnHeaderTitleGUI(Rect titleRect, string header)
{
if (m_InspectedTypes.Count > 1)
header = string.Format("Multiple Types Presets ({0})", targets.Length);
else if (targets.Length > 1)
header = string.Format("{0} Presets ({1})", m_SelectedPresetTypeName, targets.Length);
base.OnHeaderTitleGUI(titleRect, header);
}
public override bool UseDefaultMargins()
{
return false;
}
void GenerateInternalEditor()
{
Object[] objs = new Object[targets.Length];
for (var index = 0; index < targets.Length; index++)
{
var p = (Preset)targets[index];
ReferenceCount reference = null;
if (!s_References.TryGetValue(p.GetInstanceID(), out reference))
{
reference = new ReferenceCount()
{
count = 0,
reference = p.GetReferenceObject()
};
if (reference.reference == null)
{
// fast exit on NULL targets as we do not support their inspector in Preset.
m_NotSupportedEditorName = p.GetTargetTypeName();
return;
}
s_References.Add(p.GetInstanceID(), reference);
}
reference.count++;
objs[index] = reference.reference;
}
m_InternalEditor = CreateEditor(objs);
m_InternalEditor.firstInspectedEditor = true;
}
void DestroyInternalEditor()
{
if (m_InternalEditor != null)
{
DestroyImmediate(m_InternalEditor);
for (var index = 0; index < targets.Length; index++)
{
var p = (Preset)targets[index];
if (--s_References[p.GetInstanceID()].count == 0)
{
if (s_References[p.GetInstanceID()].reference is Component)
{
var go = ((Component)s_References[p.GetInstanceID()].reference).gameObject;
go.hideFlags = HideFlags.None; // make sure we remove the don't destroy flag before calling destroy
DestroyImmediate(go);
}
else
{
DestroyImmediate(s_References[p.GetInstanceID()].reference);
}
s_References.Remove(p.GetInstanceID());
}
}
}
}
void DrawInternalInspector()
{
using (var change = new EditorGUI.ChangeCheckScope())
{
if (m_InternalEditor.target is Component)
{
bool wasVisible = InternalEditorUtility.GetIsInspectorExpanded(m_InternalEditor.target);
bool isVisible = EditorGUILayout.InspectorTitlebar(wasVisible, m_InternalEditor.targets, m_InternalEditor.CanBeExpandedViaAFoldout());
if (isVisible != wasVisible)
{
InternalEditorUtility.SetIsInspectorExpanded(m_InternalEditor.target, isVisible);
}
}
else
{
m_InternalEditor.DrawHeader();
}
if (InternalEditorUtility.GetIsInspectorExpanded(m_InternalEditor.target) || m_InternalEditor.HasLargeHeader())
{
EditorGUI.indentLevel++;
m_InternalEditor.OnInspectorGUI();
EditorGUI.indentLevel--;
}
if (change.changed || m_InternalEditor.isInspectorDirty)
{
for (int i = 0; i < m_InternalEditor.targets.Length; i++)
{
((Preset)targets[i]).UpdateProperties(m_InternalEditor.targets[i]);
}
}
}
}
void DrawUnsupportedInspector()
{
GUILayout.BeginHorizontal(Style.inspectorBig);
GUILayout.Space(38);
GUILayout.BeginVertical();
GUILayout.Space(k_HeaderHeight);
GUILayout.BeginHorizontal();
EditorGUILayout.GetControlRect();
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUILayout.EndHorizontal();
Rect r = GUILayoutUtility.GetLastRect();
// Icon
Rect iconRect = new Rect(r.x + 6, r.y + 6, 32, 32);
GUI.Label(iconRect, AssetPreview.GetMiniTypeThumbnail(typeof(Object)), Style.centerStyle);
// Title
Rect titleRect = new Rect(r.x + 44, r.y + 6, r.width - 86, 16);
GUI.Label(titleRect, m_NotSupportedEditorName, EditorStyles.largeLabel);
EditorGUILayout.HelpBox("Preset Inspectors are not supported for this type.", MessageType.Info);
}
}
}