- Home /
Editor scripting - How to show the Assets menu as a context menu
I have an editor script that lets me select project files, similarly to what happens in Unity's project view. When I right click the selected files, I want to display the Assets menu as the context menu, just like Unity does in the project view:
Is there a way to do this? I know I can replicate the Assets menu item by item, using something like the following:
var menu = new GenericMenu ();
(...)
menu.AddItem (new GUIContent ("Show in Explorer"),
false, () => EditorApplication.ExecuteMenuItem("Assets/Show in Explorer"));
menu.AddItem (new GUIContent ("Open"),
false, () => EditorApplication.ExecuteMenuItem("Assets/Open"));
(...)
menu.ShowAsContext ();
But this is unmaintainable, because there are a lot of menu items (especially in the Create submenu) and every time Unity changes the Assets menu (or another editor script adds a menu item), I will have to change code to be compatible.
Can this be done in a generic way? Maybe even using reflection. Having access to the way Unity's project view does things would be a blessing.
Thanks!
Answer by VEA_Games · Nov 17, 2016 at 12:09 PM
There's a way to show any built-in menu using https://docs.unity3d.com/ScriptReference/EditorUtility.DisplayPopupMenu.html
Hi there! Sorry for the delay, but I was just able to try this out and it works perfectly! Thank you very much. I didn't know this utility existed. Accepted and upvoted. ;)
Your answer
Follow this Question
Related Questions
Unity Editor: Calling EditorCoroutine from MenuItem 2 Answers
Editor Scripting Question 1 Answer
Retrieving Project Name 4 Answers
Drop down selection list 1 Answer