Skip to content

Commit e9982ec

Browse files
committed
feat: Record MenuItems
1 parent a2583d2 commit e9982ec

File tree

7 files changed

+167
-28
lines changed

7 files changed

+167
-28
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: 24 additions & 1 deletion
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 urlHandle = new AssetHandle
128+
{
129+
_category = AssetCategory.MenuItem,
130+
_guid = menuPath,
131+
_fallbackName = title ?? string.Empty,
132+
};
133+
134+
error = null;
135+
return urlHandle;
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);
@@ -361,6 +382,7 @@ public int GetAssetInstanceId()
361382

362383
case AssetCategory.ExternalFile:
363384
case AssetCategory.Url:
385+
case AssetCategory.MenuItem:
364386
return 0;
365387

366388
default:
@@ -408,6 +430,7 @@ public string GetDisplayName()
408430
}
409431

410432
case AssetCategory.Url:
433+
case AssetCategory.MenuItem:
411434
if (string.IsNullOrEmpty(_fallbackName))
412435
{
413436
return _guid;

Editor/Scripts/AssetItemView.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ public void Bind(AssetHandle target)
129129
categoryIconTooltip = "URL";
130130
break;
131131

132+
case AssetCategory.MenuItem:
133+
assetIconTex = GetMenuItemTexture();
134+
categoryIconTex = assetIconTex;
135+
categoryIconTooltip = "MenuItem";
136+
break;
137+
132138
default:
133139
throw new ArgumentOutOfRangeException(nameof(AssetHandle.Category), AssetHandle.Category, null);
134140
}
@@ -235,6 +241,10 @@ private void OnContextClick(Vector2 mousePosition)
235241
ShowUrlContextMenu(mousePosition);
236242
break;
237243

244+
case AssetCategory.MenuItem:
245+
ShowMenuItemContextMenu(mousePosition);
246+
break;
247+
238248
default:
239249
throw new ArgumentOutOfRangeException(nameof(AssetHandle.Category), AssetHandle.Category, null);
240250
}
@@ -321,6 +331,16 @@ private void ShowUrlContextMenu(Vector2 mousePosition)
321331
genericMenu.ShowAsContext();
322332
}
323333

334+
private void ShowMenuItemContextMenu(Vector2 mousePosition)
335+
{
336+
Assert.IsTrue(AssetHandle.Category == AssetCategory.MenuItem);
337+
338+
GenericMenu genericMenu = new GenericMenu();
339+
genericMenu.AddItem(new GUIContent("Execute"), false, AssetHandle.OpenAsset);
340+
genericMenu.AddItem(new GUIContent("Remove"), false, () => OnWantsToRemoveAssetItem?.Invoke(AssetHandle));
341+
genericMenu.ShowAsContext();
342+
}
343+
324344

325345
#region Static Textures
326346

@@ -329,6 +349,7 @@ private void ShowUrlContextMenu(Vector2 mousePosition)
329349
private static Texture _externalFileTextureCache;
330350
private static Texture _externalFileTextureSmallCache;
331351
private static Texture _urlTextureCache;
352+
private static Texture _menuItemTextureCache;
332353
private static Texture _warningTextureCache;
333354

334355
private static Texture GetObjectIcon(UObject obj, SceneAsset containingScene)
@@ -412,6 +433,16 @@ private static Texture GetUrlTexture()
412433
return _urlTextureCache;
413434
}
414435

436+
private static Texture GetMenuItemTexture()
437+
{
438+
if (!_menuItemTextureCache)
439+
{
440+
_menuItemTextureCache = (Texture)EditorGUIUtility.Load(EditorGUIUtility.isProSkin ? "d_PlayButton@2x" : "PlayButton@2x");
441+
}
442+
443+
return _menuItemTextureCache;
444+
}
445+
415446
private static Texture GetWarningTexture()
416447
{
417448
if (!_warningTextureCache)

Editor/Scripts/AssetQuickAccessLocalCache.cs

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

187+
public bool AddMenuItems(IEnumerable<(string url, string title)> menuItemInfos, ref StringBuilder errorsBuilder, bool clearErrorsBuilder)
188+
{
189+
if (clearErrorsBuilder)
190+
{
191+
errorsBuilder?.Clear();
192+
}
193+
194+
bool added = false;
195+
foreach ((string menuPath, string title) menuItemInfo in menuItemInfos)
196+
{
197+
if (_assetHandles.Any(h => h.GetAssetPath() == menuItemInfo.menuPath))
198+
{
199+
errorsBuilder ??= new StringBuilder();
200+
errorsBuilder.AppendLine("Menu item already exists.");
201+
continue;
202+
}
203+
204+
AssetHandle handle = AssetHandle.CreateFromMenuPath(menuItemInfo.menuPath, menuItemInfo.title, out string error);
205+
if (!string.IsNullOrEmpty(error))
206+
{
207+
errorsBuilder ??= new StringBuilder();
208+
errorsBuilder.AppendLine(error);
209+
}
210+
211+
if (handle == null)
212+
{
213+
continue;
214+
}
215+
216+
_assetHandles.Add(handle);
217+
added = true;
218+
}
219+
220+
if (added)
221+
{
222+
ForceSave();
223+
}
224+
225+
return added;
226+
}
227+
187228
public bool RemoveAsset(AssetHandle handle)
188229
{
189230
if (_assetHandles.Remove(handle))

0 commit comments

Comments
 (0)