Skip to content

Commit 2f88237

Browse files
committed
feat: Record menu items
1 parent e50b871 commit 2f88237

14 files changed

+517
-12
lines changed

Editor/Scripts/AssetCategory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public enum AssetCategory
1010
SceneObject = 2,
1111
ExternalFile = 4,
1212
Url = 8,
13+
MenuItem = 16,
1314
}
1415
}

Editor/Scripts/AssetHandle.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ public static AssetHandle CreateFromUrl(string url, string title, out string err
122122
return urlHandle;
123123
}
124124

125+
public static AssetHandle CreateFromMenuPath(string menuPath, string title, out string error)
126+
{
127+
AssetHandle menuItemHandle = new AssetHandle
128+
{
129+
_category = AssetCategory.MenuItem,
130+
_guid = menuPath,
131+
_fallbackName = title ?? string.Empty,
132+
};
133+
134+
error = null;
135+
return menuItemHandle;
136+
}
137+
125138
public static void ForceSaveLocalCache()
126139
{
127140
AssetQuickAccessLocalCache.instance.ForceSave();
@@ -165,6 +178,7 @@ public void Update()
165178

166179
case AssetCategory.ExternalFile:
167180
case AssetCategory.Url:
181+
case AssetCategory.MenuItem:
168182
break;
169183

170184
default:
@@ -227,6 +241,10 @@ public void OpenAsset()
227241
Application.OpenURL(_guid);
228242
break;
229243

244+
case AssetCategory.MenuItem:
245+
EditorApplication.ExecuteMenuItem(_guid);
246+
break;
247+
230248
default:
231249
throw new ArgumentOutOfRangeException(nameof(Category), Category, null);
232250
}
@@ -273,6 +291,7 @@ public string GetAssetName()
273291
return Path.GetFileName(GetAssetPath());
274292

275293
case AssetCategory.Url:
294+
case AssetCategory.MenuItem:
276295
return _guid;
277296

278297
default:
@@ -317,6 +336,7 @@ public string GetAssetPath()
317336

318337
case AssetCategory.ExternalFile:
319338
case AssetCategory.Url:
339+
case AssetCategory.MenuItem:
320340
return _guid;
321341

322342
default:
@@ -339,7 +359,8 @@ public string GetAssetTypeFullName()
339359

340360
case AssetCategory.ExternalFile:
341361
case AssetCategory.Url:
342-
return null;
362+
case AssetCategory.MenuItem:
363+
return typeof(string).FullName;
343364

344365
default:
345366
throw new ArgumentOutOfRangeException(nameof(Category), Category, null);
@@ -354,12 +375,14 @@ public int GetAssetInstanceId()
354375
case AssetCategory.SceneObject:
355376
if (_asset)
356377
{
357-
return _asset.GetInstanceID();
378+
int instanceId = _asset.GetInstanceID();
379+
return instanceId;
358380
}
359381
return 0;
360382

361383
case AssetCategory.ExternalFile:
362384
case AssetCategory.Url:
385+
case AssetCategory.MenuItem:
363386
return 0;
364387

365388
default:
@@ -407,6 +430,7 @@ public string GetDisplayName()
407430
}
408431

409432
case AssetCategory.Url:
433+
case AssetCategory.MenuItem:
410434
if (string.IsNullOrEmpty(_fallbackName))
411435
{
412436
return _guid;

Editor/Scripts/AssetQuickAccessLocalCache.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,47 @@ public bool AddUrls(IEnumerable<(string url, string title)> urlInfos, ref String
213213
return added;
214214
}
215215

216+
public bool AddMenuItems(IEnumerable<(string menuPath, string title)> menuItemInfos, ref StringBuilder errorsBuilder, bool clearErrorsBuilder)
217+
{
218+
if (clearErrorsBuilder)
219+
{
220+
errorsBuilder?.Clear();
221+
}
222+
223+
bool added = false;
224+
foreach ((string menuPath, string title) menuItemInfo in menuItemInfos)
225+
{
226+
if (_assetHandles.Any(h => h.GetAssetPath() == menuItemInfo.menuPath))
227+
{
228+
errorsBuilder = errorsBuilder ?? new StringBuilder();
229+
errorsBuilder.AppendLine("Menu item already exists.");
230+
continue;
231+
}
232+
233+
AssetHandle handle = AssetHandle.CreateFromMenuPath(menuItemInfo.menuPath, menuItemInfo.title, out string error);
234+
if (!string.IsNullOrEmpty(error))
235+
{
236+
errorsBuilder = errorsBuilder ?? new StringBuilder();
237+
errorsBuilder.AppendLine(error);
238+
}
239+
240+
if (handle == null)
241+
{
242+
continue;
243+
}
244+
245+
_assetHandles.Add(handle);
246+
added = true;
247+
}
248+
249+
if (added)
250+
{
251+
ForceSave();
252+
}
253+
254+
return added;
255+
}
256+
216257
public bool RemoveAsset(AssetHandle handle)
217258
{
218259
if (_assetHandles.Remove(handle))

Editor/Scripts/AssetQuickAccessWindow.BottomContent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private void DrawBottomTips()
3939
private void DrawFindObjectContent()
4040
{
4141
GUILayout.BeginHorizontal();
42-
EditorGUILayout.LabelField("Path/Guid/InstanceId", GUILayout.Width(120));
42+
EditorGUILayout.LabelField("Path/Guid/InstanceId", GUILayout.Width(128));
4343
_assetIdentifier = EditorGUILayout.TextField(_assetIdentifier);
4444
EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(_assetIdentifier));
4545
if (GUILayout.Button("Find", GUILayout.Width(60)))

Editor/Scripts/AssetQuickAccessWindow.ListContent.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ private void DrawAssetListItem(Rect rect, int index, bool isActive, bool isFocus
100100
// categoryIconTooltip = "URL";
101101
break;
102102

103+
case AssetCategory.MenuItem:
104+
// string url = assetHandle.GetAssetPath();
105+
assetIconTex = TextureUtility.GetMenuItemTexture();
106+
categoryIconTex = assetIconTex;
107+
// categoryIconTooltip = "MenuItem";
108+
break;
109+
103110
default:
104111
throw new ArgumentOutOfRangeException(nameof(assetHandle.Category), assetHandle.Category, null);
105112
}
@@ -217,6 +224,10 @@ private void OnRightClickAssetListItem(AssetHandle handle)
217224
ShowUrlContextMenu(handle);
218225
break;
219226

227+
case AssetCategory.MenuItem:
228+
ShowMenuItemContextMenu(handle);
229+
break;
230+
220231
default:
221232
throw new ArgumentOutOfRangeException(nameof(handle.Category), handle.Category, null);
222233
}
@@ -313,6 +324,16 @@ private void ShowUrlContextMenu(AssetHandle assetHandle)
313324
genericMenu.ShowAsContext();
314325
}
315326

327+
private void ShowMenuItemContextMenu(AssetHandle assetHandle)
328+
{
329+
Assert.IsTrue(assetHandle.Category == AssetCategory.MenuItem);
330+
331+
GenericMenu genericMenu = new GenericMenu();
332+
genericMenu.AddItem(new GUIContent("Execute"), false, assetHandle.OpenAsset);
333+
genericMenu.AddItem(new GUIContent("Remove"), false, () => RemoveAsset(assetHandle));
334+
genericMenu.ShowAsContext();
335+
}
336+
316337
private void ProcessAssetItemAction(Rect dragArea, AssetHandle assetHandle)
317338
{
318339
Event e = Event.current;

Editor/Scripts/AssetQuickAccessWindow.OnGUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private void ProcessDragAndDropIn()
4747

4848
if (!DragGenericData.Equals(DragAndDrop.GetGenericData(DragGenericData)))
4949
{
50-
AddItems(DragAndDrop.objectReferences, DragAndDrop.paths, null);
50+
AddItems(DragAndDrop.objectReferences, DragAndDrop.paths, null, null);
5151
}
5252
}
5353
}

Editor/Scripts/AssetQuickAccessWindow.Toolbar.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ namespace GBG.AssetQuickAccess.Editor
55
{
66
public partial class AssetQuickAccessWindow
77
{
8-
private static readonly string[] _toolbarCategoryNames = { "All", "Assets", "Scene Objects", "External Items" };
8+
private static readonly string[] _toolbarCategoryNames =
9+
{
10+
"All", "Assets", "Scene Objects", "External Items", "Menu Items"
11+
};
912
private GUIContent _toolbarMenuContent;
1013
private GUIStyle _toolbarMenuStyle;
1114

@@ -34,6 +37,11 @@ private void DrawToolbar()
3437
SelectCategory(AssetCategory.ExternalFile | AssetCategory.Url);
3538
}
3639

40+
if (GUILayout.Toggle(LocalCache.SelectedCategories == AssetCategory.MenuItem, _toolbarCategoryNames[4]))
41+
{
42+
SelectCategory(AssetCategory.MenuItem);
43+
}
44+
3745
if (_toolbarMenuContent == null)
3846
{
3947
_toolbarMenuContent = EditorGUIUtility.IconContent(EditorGUIUtility.isProSkin ? "d__Menu" : "_Menu");
@@ -53,6 +61,7 @@ private void DrawToolbar()
5361
toolbarMenu.AddItem(new GUIContent("Add External File"), false, AddExternalFile);
5462
toolbarMenu.AddItem(new GUIContent("Add External Folder"), false, AddExternalFolder);
5563
toolbarMenu.AddItem(new GUIContent("Add URL"), false, AddUrlEditor);
64+
toolbarMenu.AddItem(new GUIContent("Add MenuItem"), false, AddMenuItemEditor);
5665
toolbarMenu.AddSeparator("");
5766
toolbarMenu.AddItem(new GUIContent("Remove All Items"), false, RemoveAllItems);
5867
toolbarMenu.ShowAsContext();

Editor/Scripts/AssetQuickAccessWindow.cs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public static void Open()
2121
GetWindow<AssetQuickAccessWindow>();
2222
}
2323

24-
public static void AddItems(IList<UObject> objects, IList<string> paths, IList<(string url, string title)> urlInfos)
24+
public static void AddItems(IList<UObject> objects, IList<string> paths,
25+
IList<(string url, string title)> urlInfos, IList<(string menuPath, string title)> menuItemInfos)
2526
{
2627
HashSet<UObject> objectHashSet = new HashSet<UObject>();
2728
if (objects != null)
@@ -80,6 +81,21 @@ public static void AddItems(IList<UObject> objects, IList<string> paths, IList<(
8081
added |= AssetQuickAccessLocalCache.instance.AddUrls(urlDict.Values, ref errorsBuilder, false);
8182
}
8283

84+
if (menuItemInfos != null)
85+
{
86+
Dictionary<string, (string menuPath, string title)> menuDict = new Dictionary<string, (string menuPath, string title)>();
87+
for (int i = 0; i < menuItemInfos.Count; i++)
88+
{
89+
string menuPath = menuItemInfos[i].menuPath;
90+
if (!menuDict.ContainsKey(menuPath))
91+
{
92+
menuDict.Add(menuPath, menuItemInfos[i]);
93+
}
94+
}
95+
96+
added |= AssetQuickAccessLocalCache.instance.AddMenuItems(menuDict.Values, ref errorsBuilder, false);
97+
}
98+
8399
if (_instance)
84100
{
85101
if (added)
@@ -100,7 +116,7 @@ public static void AddItems(IList<UObject> objects, IList<string> paths, IList<(
100116
#endif
101117
public static void AddSelectedObjects()
102118
{
103-
AddItems(Selection.objects, null, null);
119+
AddItems(Selection.objects, null, null, null);
104120
}
105121

106122
[MenuItem("Assets/Bamboo/Add to Asset Quick Access", validate = true)]
@@ -113,7 +129,7 @@ public static void AddContextObject(MenuCommand command)
113129
{
114130
if (command.context)
115131
{
116-
AddItems(new UObject[] { command.context }, null, null);
132+
AddItems(new UObject[] { command.context }, null, null, null);
117133
}
118134
}
119135
#endif
@@ -274,14 +290,31 @@ private void AddUrlEditor()
274290
UrlEditWindow.Open(center, AddUrl);
275291
}
276292

293+
private void AddMenuItemEditor()
294+
{
295+
Vector2 upperCenter = position.center;
296+
upperCenter.y = position.yMin + 50;
297+
MenuItemSelectWindow.Open(upperCenter, AddMenuItem);
298+
}
299+
277300
private void AddUrl(string url, string title)
278301
{
279302
if (string.IsNullOrWhiteSpace(url))
280303
{
281304
return;
282305
}
283306

284-
AddItems(null, null, new (string url, string title)[] { (url, title) });
307+
AddItems(null, null, new (string url, string title)[] { (url, title) }, null);
308+
}
309+
310+
private void AddMenuItem(string menuPath, string title)
311+
{
312+
if (string.IsNullOrWhiteSpace(menuPath))
313+
{
314+
return;
315+
}
316+
317+
AddItems(null, null, null, new (string menuPath, string title)[] { (menuPath, title) });
285318
}
286319

287320
#endregion

0 commit comments

Comments
 (0)