-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathAddressableBrowser.cs
More file actions
413 lines (372 loc) · 17.1 KB
/
Copy pathAddressableBrowser.cs
File metadata and controls
413 lines (372 loc) · 17.1 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#if TK_ADDRESSABLE
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using ThunderKit.Common;
using ThunderKit.Core.Windows;
using UnityEditor;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.ResourceManagement.ResourceLocations;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using UnityEditor.Experimental.SceneManagement;
using UnityEngine.Rendering;
#if UNITY_2019_1_OR_NEWER
using UnityEngine.UIElements;
using UnityEditor.UIElements;
#else
using UnityEditor.Experimental.UIElements;
using UnityEngine.Experimental.UIElements;
#endif
using Object = UnityEngine.Object;
namespace ThunderKit.Addressable.Tools
{
using static ThunderKit.Core.UIElements.TemplateHelpers;
[Flags]
public enum BrowserOptions
{
None = 0x0,
ShowType = 0x1,
ShowProvider = 0x2,
IgnoreCase = 0x4,
UseRegex = 0x8
}
public class AddressableBrowser : TemplatedWindow
{
[MenuItem(Constants.ThunderKitMenuRoot + "Addressable Browser")]
public static void ShowAddressableBrowser() => GetWindow<AddressableBrowser>();
public override string Title { get => ObjectNames.NicifyVariableName(GetType().Name); }
private const string CopyButton = "addressable-copy-button";
private const string NameLabel = "addressable-label";
private const string TypeLabel = "addressable-type-label";
private const string ProviderLabel = "addressable-provider-label";
private const string PreviewIcon = "addressable-icon";
private const string AddressableAssetName = "addressable-asset";
private const string ButtonPanel = "addressable-button-panel";
private const string LoadSceneButton = "addressable-load-scene-button";
private const string InspectButton = "addressable-inspect-button";
private const string AddressableLabels = "addressable-labels";
Dictionary<string, List<IResourceLocation>> DirectoryContents;
private ListView directory;
private ListView directoryContent;
private TextField searchBox;
private EnumFlagsField displayOptionsField;
private Button helpButton;
public string searchInput;
public BrowserOptions browserOptions = BrowserOptions.ShowType | BrowserOptions.ShowProvider | BrowserOptions.IgnoreCase;
private object[] loadParams = new object[1];
private Type[] loadParamTypes = new Type[] { typeof(IResourceLocation) };
private Regex regex;
private string typeFilter;
private List<IResourceLocation> LoadIndex()
{
var set = new HashSet<IResourceLocation>();
if (!Addressables.ResourceLocators.Any()) return set.ToList();
foreach (var locator in Addressables.ResourceLocators)
{
switch (locator)
{
case ResourceLocationMap rlm:
foreach (var location in rlm.Locations.SelectMany(loc => loc.Value).Where(val => val.ResourceType != typeof(IAssetBundleResource)))
set.Add(location);
break;
}
}
return set.ToList();
}
public void OnDisable() => AddressableGraphicsSettings.AddressablesInitialized -= InitializeBrowser;
public override void OnEnable()
{
AddressableGraphicsSettings.AddressablesInitialized -= InitializeBrowser;
AddressableGraphicsSettings.AddressablesInitialized += InitializeBrowser;
InitializeBrowser();
}
private void InitializeBrowser(object sender = null, EventArgs e = null)
{
base.OnEnable();
searchBox = rootVisualElement.Q<TextField>("search-input");
displayOptionsField = rootVisualElement.Q<EnumFlagsField>("display-options");
directory = rootVisualElement.Q<ListView>("directory");
directoryContent = rootVisualElement.Q<ListView>("directory-content");
helpButton = rootVisualElement.Q<Button>("help-button");
helpButton.clickable = new Clickable(() =>
{
Documentation.ShowThunderKitDocumentation("Packages/com.passivepicasso.thunderkit/Documentation/ThunderKitDocumentation/Manual/AddressableBrowser.md");
});
directory.itemsSource = new ArrayList();
directoryContent.itemsSource = new ArrayList();
directory.makeItem = DirectoryLabel;
directoryContent.makeItem = LoadAssetTemplate;
directory.bindItem = (element, i) =>
{
Label label = element.Q<Label>(NameLabel);
var location = (IResourceLocation)directory.itemsSource[i];
label.text = GroupLocation(location);
};
directoryContent.bindItem = BindAsset;
List<IResourceLocation> resourceLocations = LoadIndex();
DirectoryContents = resourceLocations.GroupBy(GroupLocation).ToDictionary(g => g.Key, g => g.Distinct().ToList());
DirectoryContents["Scenes"] = resourceLocations.Where(location => location.ResourceType == typeof(SceneInstance)).ToList();
#if UNITY_2020_1_OR_NEWER
directory.onSelectionChange += Directory_onSelectionChanged;
directoryContent.onSelectionChange += DirectoryContent_onSelectionChanged;
#else
directory.onSelectionChanged += Directory_onSelectionChanged;
directoryContent.onSelectionChanged += DirectoryContent_onSelectionChanged;
#endif
#if UNITY_2019_1_OR_NEWER
searchBox.RegisterValueChangedCallback(OnSearchChanged);
displayOptionsField.RegisterValueChangedCallback(OnOptionsChanged);
#else
searchBox.OnValueChanged(OnSearchChanged);
regexOptionsField.OnValueChanged(OnRegexOptionsChanged);
#endif
RefreshSearch();
}
private void OnOptionsChanged(ChangeEvent<System.Enum> evt) => RefreshSearch();
private void OnSearchChanged(ChangeEvent<string> evt) => RefreshSearch();
private void RefreshSearch()
{
bool noFilter = string.IsNullOrEmpty(searchInput);
var searchValue = searchInput ?? string.Empty;
var typeIndex = searchValue.IndexOf("t:");
if (typeIndex > -1 && (typeIndex - 1 == -1 || searchValue[typeIndex - 1] == ' '))
{
typeFilter = searchValue.Substring(typeIndex);
int spaceIndex = typeFilter.IndexOf(" ");
if (spaceIndex == -1)
{
searchValue = searchValue.Replace(typeFilter, string.Empty);
if (searchValue.EndsWith(" "))
searchValue = searchValue.Substring(0, searchValue.Length - 1);
typeFilter = typeFilter.Substring(2);
}
else
{
searchValue = searchValue.Replace(typeFilter.Substring(0, spaceIndex + 1), string.Empty);
typeFilter = typeFilter.Substring(2, spaceIndex - 2);
}
}
else typeFilter = string.Empty;
if (!browserOptions.HasFlag(BrowserOptions.UseRegex)) searchValue = Regex.Escape(searchValue);
if (string.IsNullOrEmpty(searchValue))
regex = null;
else
{
var regexOptions = browserOptions.HasFlag(BrowserOptions.IgnoreCase) ? RegexOptions.IgnoreCase : RegexOptions.None;
regex = new Regex(searchValue, regexOptions);
}
EditorApplication.update += Refresh;
}
private void Refresh()
{
EditorApplication.update -= Refresh;
directory.itemsSource.Clear();
foreach (var kvp in DirectoryContents)
Filter(directory.itemsSource, kvp.Value, true);
((ArrayList)directory.itemsSource).Sort(comparer);
#if UNITY_2021_2_OR_NEWER
directory.Rebuild();
directoryContent.Rebuild();
#else
directory.Refresh();
directoryContent.Refresh();
#endif
}
ResoourceNameSorter comparer = new ResoourceNameSorter();
private class ResoourceNameSorter : IComparer
{
public int Compare(object x, object y)
{
if (x == null || y == null) return 0;
if (x is IResourceLocation xirl && y is IResourceLocation yirl)
return string.Compare(xirl.PrimaryKey, yirl.PrimaryKey);
return 0;
}
}
private void Directory_onSelectionChanged(IEnumerable<object> obj)
{
var selected = GroupLocation(obj.OfType<IResourceLocation>().First());
var locations = DirectoryContents[selected];
directoryContent.itemsSource.Clear();
Filter(directoryContent.itemsSource, locations, false);
#if UNITY_2021_2_OR_NEWER
directoryContent.Rebuild();
#else
directoryContent.Refresh();
#endif
}
private string GroupLocation(IResourceLocation g)
{
if (g.ResourceType == typeof(SceneInstance))
{
return "Scenes";
}
else
{
var lastIndexForwardslash = g.PrimaryKey.LastIndexOf("/");
var lastIndexBackslash = g.PrimaryKey.LastIndexOf("\\");
if (lastIndexForwardslash > -1 || lastIndexBackslash > -1)
{
if (lastIndexBackslash > lastIndexForwardslash)
return g.PrimaryKey.Substring(0, lastIndexBackslash);
else
return g.PrimaryKey.Substring(0, lastIndexForwardslash);
}
else
return "Assorted";
}
}
private void Filter(IList list, List<IResourceLocation> values, bool earlyReturn)
{
foreach (var location in values)
{
if (!typeof(Object).IsAssignableFrom(location.ResourceType) &&
!typeof(SceneInstance).IsAssignableFrom(location.ResourceType)) continue;
if (regex != null && !regex.IsMatch(location.PrimaryKey)) continue;
if (!string.IsNullOrEmpty(typeFilter) && !location.ResourceType.FullName.Contains(typeFilter)) continue;
list.Add(location);
if (earlyReturn)
break;
}
}
private void BindAsset(VisualElement element, int i)
{
var location = (IResourceLocation)directoryContent.itemsSource[i];
var icon = element.Q<AddressablePreviewImage>(PreviewIcon);
var address = location.PrimaryKey;
var isAsset = IsLoadableAsset(location);
var nameLabel = element.Q<TextField>(NameLabel);
nameLabel.SetValueWithoutNotify(address);
if (browserOptions.HasFlag(BrowserOptions.ShowType))
{
var typeLabel = element.Q<TextField>(TypeLabel);
typeLabel.SetValueWithoutNotify(location.ResourceType.FullName);
}
if (browserOptions.HasFlag(BrowserOptions.ShowProvider))
{
var providerLabel = element.Q<TextField>(ProviderLabel);
providerLabel.SetValueWithoutNotify(location.ProviderId);
switch (location.ProviderId)
{
case "UnityEngine.ResourceManagement.ResourceProviders.LegacyResourcesProvider":
providerLabel.tooltip = "Cannot load assets provided by LegacyResourcesProvider";
break;
// case "UnityEngine.ResourceManagement.ResourceProviders.BundledAssetProvider":
// providerLabel.tooltip = location.Dependencies
// .Select(dep => dep.PrimaryKey)
// .Aggregate("Dependencies:",
// (a, b) => $"{a}\r\n{b}");
// break;
}
}
var loadSceneBtn = element.Q<Button>(LoadSceneButton);
var inspectBtn = element.Q<Button>(InspectButton);
inspectBtn.style.display = DisplayStyle.None;
loadSceneBtn.style.display = DisplayStyle.None;
if (typeof(GameObject).IsAssignableFrom(location.ResourceType))
{
inspectBtn.style.display = DisplayStyle.Flex;
inspectBtn.clickable = new Clickable(() =>
{
var handle = Addressables.LoadAssetAsync<GameObject>(location);
EditorApplication.CallbackFunction updateSceneView = null;
updateSceneView = new EditorApplication.CallbackFunction(UpdateSceneView);
EditorApplication.update += updateSceneView;
void UpdateSceneView()
{
if (handle.IsDone)
{
EditorApplication.update -= updateSceneView;
AddressablePreviewStage.Show(handle.Result);
Addressables.Release(handle);
}
if (SceneView.lastActiveSceneView)
{
SceneView.lastActiveSceneView.sceneLighting = false;
SceneView.lastActiveSceneView.Repaint();
}
}
});
}
else if (location.ResourceType == typeof(SceneInstance))
{
loadSceneBtn.style.display = DisplayStyle.Flex;
if (EditorApplication.isPlaying)
{
loadSceneBtn.clickable = new Clickable(() =>
{
var currentAddress = directoryContent.itemsSource[i] as IResourceLocation;
var key = currentAddress.ToString();
Addressables.LoadSceneAsync(currentAddress);
});
}
else
{
loadSceneBtn.clickable = null;
}
}
_ = icon.Render(location, address, isAsset);
}
private bool IsLoadableAsset(IResourceLocation location) =>
location.ResourceType != typeof(SceneInstance)
&& location.ResourceType != typeof(IAssetBundleResource)
&& location.ProviderId != "UnityEngine.ResourceManagement.ResourceProviders.LegacyResourcesProvider"
&& typeof(Object).IsAssignableFrom(location.ResourceType);
private MethodInfo GetLoadMethod(IResourceLocation location)
{
var addressablesType = typeof(Addressables);
var loadAsyncMethodTemplate = addressablesType.GetMethod(nameof(Addressables.LoadAssetAsync), loadParamTypes);
var loadAssetAsync = loadAsyncMethodTemplate.MakeGenericMethod(location.ResourceType);
return loadAssetAsync;
}
private void DirectoryContent_onSelectionChanged(IEnumerable<object> obj)
{
try
{
var location = obj.OfType<IResourceLocation>().First();
var isLoadable = IsLoadableAsset(location);
if (!isLoadable)
return;
var loadAssetAsync = GetLoadMethod(location);
loadParams[0] = location;
var firstOp = loadAssetAsync.Invoke(null, loadParams);
var asyncOpType = firstOp.GetType();
var asyncOpWaitForCompletionMethod = asyncOpType.GetMethod("WaitForCompletion");
var firstObj = asyncOpWaitForCompletionMethod.Invoke(firstOp, null);
if (firstObj is Object uniObj)
{
uniObj.hideFlags |= HideFlags.NotEditable;
Selection.activeObject = uniObj;
}
else
Debug.LogWarning("Loaded object is not a UnityEngine.Object");
}
catch (Exception e)
{
Debug.LogError(e);
}
}
private VisualElement DirectoryLabel()
{
var label = new Label { name = NameLabel };
label.AddToClassList("addresable-label");
return label;
}
private VisualElement LoadAssetTemplate()
{
var element = GetTemplateInstance("AddressableAsset");
var labelContainer = element.Q(AddressableLabels);
if (browserOptions.HasFlag(BrowserOptions.ShowType)) /**/labelContainer.Q(TypeLabel/**/).style.display = DisplayStyle.Flex;
if (browserOptions.HasFlag(BrowserOptions.ShowProvider)) labelContainer.Q(ProviderLabel).style.display = DisplayStyle.Flex;
return element;
}
}
}
#endif