-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathPopupEditorWindow.cs
291 lines (245 loc) · 8.89 KB
/
PopupEditorWindow.cs
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
using static UnityEditor.AssetDatabase;
namespace cratesmith.assetui
{
public class PopupEditorWindow : EditorWindow
{
private Editor m_editor;
private List<Editor> m_subEditors = new List<Editor>();
[SerializeField] private Vector2 m_scrollPosition = Vector2.zero;
[SerializeField] private Object m_target;
[SerializeField] bool m_Pinned;
static Texture2D s_PinButton;
static Texture2D PinButton => s_PinButton
? s_PinButton
: s_PinButton = LoadAssetAtPath<Texture2D>(
GUIDToAssetPath(
FindAssets("popout_pin t:texture").FirstOrDefault()));
private const string MENUITEM_WINDOW_STRING = "Window/View in Popup Inspector... &\\";
private const string MENUITEM_PREFABS_STRING = "Window/Go to in scene and prefabs... %t";
private const string MENUITEM_ASSETS_STRING = "Window/Go to in scriptable objects... &t";
private const string MENUITEM_ALLASSETS_STRING = "Window/Go to in all assets... %#t";
[MenuItem(MENUITEM_WINDOW_STRING, true)]
public static bool _PopupEditorWindowMenuItem()
{
return Selection.activeObject != null;
}
[MenuItem(MENUITEM_WINDOW_STRING)]
public static void PopupEditorWindowMenuItem()
{
Popup(Selection.activeObject);
}
static void Popup(Object obj)
{
if (obj is GameObject gameObject)
{
GameObjectPopup(gameObject);
} else if (obj)
Create(obj,GUIUtility.GUIToScreenPoint(Event.current.mousePosition), new Vector2(600, 500));
}
[MenuItem(MENUITEM_PREFABS_STRING)]
public static void PopupEditorWindowMenuItemPrefabs()
{
var title = "Go to gameobject or prefab";
var extraObjects = new List<Object>();
for (int i = 0; i < EditorSceneManager.sceneCount; i++)
{
var scene = EditorSceneManager.GetSceneAt(i);
if (!scene.isLoaded)
continue;
extraObjects.AddRange(scene.GetRootGameObjects()
.SelectMany(x => x.GetComponentsInChildren<Transform>()
.Select(x => x.gameObject)
.Where(x => !PrefabUtility.IsPartOfPrefabInstance(x) || PrefabUtility.IsOutermostPrefabInstanceRoot(x))));
}
AssetPopup("t:prefab", title,extraObjects);
}
[MenuItem(MENUITEM_ASSETS_STRING)]
public static void PopupEditorWindowMenuItemAssets()
{
AssetPopup("t:ScriptableObject", "Go to ScriptableObject asset");
}
[MenuItem(MENUITEM_ALLASSETS_STRING)]
public static void PopupEditorWindowMenuItemAllAssets()
{
AssetPopup("t:ScriptableObject t:prefab", "Go to asset");
}
static void AssetPopup(string filter, string title, List<Object> extraObjects=null)
{
if (extraObjects == null)
extraObjects = new List<Object>();
var options = extraObjects.Select(x => (x, x.name, ""))
.Concat(AssetDatabase.FindAssets(filter).Select(AssetDatabase.GUIDToAssetPath).Select(x => ((Object)null, Path.GetFileName(x), x))).ToArray();
OptionPopupWindow.Create(title,
id =>
{
var obj = options[id].Item1
? options[id].Item1
: AssetDatabase.LoadMainAssetAtPath(options[id].Item3);
Popup(obj);
if (Event.current.control || Event.current.shift)
{
EditorGUIUtility.PingObject(obj);
}
if (Event.current.control)
{
Selection.activeObject = obj;
}
},
options.Select(x => new GUIContent($"{x.Item2}", x.Item1 ? EditorIconUtility.GetIcon(x.Item1) : AssetDatabase.GetCachedIcon(x.Item3))).ToArray(),
extraText:"+ctrl: select, +shift:highlight");
}
static void GameObjectPopup(GameObject gameObject)
{
if (!gameObject)
return;
var options = gameObject.GetComponentsInChildren<Transform>().Select(x => x.gameObject)
.SelectMany(x => new Object[]
{
x
}.Concat(x.GetComponentsInChildren<Component>()))
.ToArray();
OptionPopupWindow.Create("Select Component/GameObject",
id => Create(options[id], GUIUtility.GUIToScreenPoint(Event.current.mousePosition), new Vector2(600, 500)),
options.Select(x => new GUIContent($"{x.GetType().Name} ({x.name})", EditorIconUtility.GetIcon(x))).ToArray());
}
static bool ImageToggle(Rect position, Texture2D buttonImg, bool value, string tooltip)
{
bool result = GUI.Toggle(position, value, new GUIContent("",tooltip), "Button");
Vector2 imgSize = new Vector2(buttonImg.width, buttonImg.height);
Rect imgRect = new Rect(position.center - imgSize / 2f, imgSize);
Color prevColor = GUI.color;
GUI.color = EditorGUIUtility.isProSkin ? Color.white : Color.gray;
GUI.DrawTexture(imgRect, buttonImg);
GUI.color = prevColor;
return result;
}
public static PopupEditorWindow Create(Object obj, Vector2 position, Vector2 size) => Create(obj, new Rect(position - Vector2.right * (size.x * .5f), size));
public static PopupEditorWindow Create(Object obj, Rect rect)
{
var window = CreateInstance<PopupEditorWindow>();
window.minSize = new Vector2(400, 500);
window.Init(obj);
window.Show();
window.Focus();
window.position = rect;
return window;
}
private void Init(Object obj)
{
m_target = obj;
m_editor = Editor.CreateEditor(m_target);
m_subEditors.Clear();
var gameObject = m_target as GameObject;
if (gameObject)
{
foreach (var component in gameObject.GetComponents<Component>())
{
m_subEditors.Add(Editor.CreateEditor(component));
}
}
var icon = EditorIconUtility.GetIcon(obj);
if (!icon) icon = AssetDatabase.GetCachedIcon(AssetDatabase.GetAssetPath(obj)) as Texture2D;
titleContent = new GUIContent(m_target.name, icon);
}
void OnEnable()
{
if (m_target)
{
Init(m_target);
}
}
void Update()
{
if (EditorWindow.focusedWindow != null
&& focusedWindow!=this
&& !(focusedWindow.GetType().Name == "ObjectSelector")
&& !(focusedWindow is OptionPopupWindow)
&& !m_Pinned)
{
Close();
}
}
void OnGUI()
{
if (m_editor==null || m_editor.target==null)
{
return;
}
m_scrollPosition = GUILayout.BeginScrollView(m_scrollPosition, GUIStyle.none);
OnGUI_DrawEditor(m_editor, true, false);
foreach (var editor in m_subEditors)
{
OnGUI_DrawEditor(editor, false, true);
}
if (docked)
m_Pinned = true;
else
m_Pinned = ImageToggle(new Rect(4, 33, 18, 18), PinButton, m_Pinned,"Keep Open");
if (!m_Pinned && Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape)
{
Close();
}
GUILayout.EndScrollView();
}
private void OnGUI_DrawEditor(Editor editor, bool drawHeader, bool isExpandable)
{
if (editor.targets.Length == 0)
{
return;
}
bool wideMode = EditorGUIUtility.wideMode;
var labelWidth = EditorGUIUtility.labelWidth;
var fieldWidth = EditorGUIUtility.fieldWidth;
EditorGUIUtility.wideMode = true;
EditorGUIUtility.labelWidth = 0;
EditorGUIUtility.fieldWidth = 0;
if (drawHeader)
{
editor.DrawHeader();
}
var style = !editor.UseDefaultMargins() ? GUIStyle.none : EditorStyles.inspectorDefaultMargins;
using (new EditorGUILayout.VerticalScope(style))
{
bool drawEditor = !isExpandable;
if (isExpandable)
{
var prevExpanded = false;
foreach (var target in editor.targets)
{
if (UnityEditorInternal.InternalEditorUtility.GetIsInspectorExpanded(target))
{
prevExpanded = true;
break;
}
}
var expanded = EditorGUILayout.InspectorTitlebar(prevExpanded, editor.targets);
if (expanded != prevExpanded)
{
foreach (var target in editor.targets)
{
UnityEditorInternal.InternalEditorUtility.SetIsInspectorExpanded(target, expanded);
}
}
drawEditor = expanded;
}
if (drawEditor)
{
editor.OnInspectorGUI();
}
}
EditorGUIUtility.labelWidth = labelWidth;
EditorGUIUtility.fieldWidth = fieldWidth;
EditorGUIUtility.wideMode = wideMode;
}
}
}
#endif