- Home /
How to add context menu on assets and asset folder?
I want to add menu in right click context on some asset. How can I do?
What kind of menu ? What kind of asset ? What have you tried ? Do you know what is uGUI ?
Do you know what Context $$anonymous$$enu is? It is a menu when you right click on things
Answer by troopy28 · May 25, 2017 at 02:37 PM
Hello!
The question is a bit old but maybe it will help other people!
You could add a menu item that will be shown on all assets, but with a validation method. Let's say you have an asset of the type "Variable". You would do something like this to add the menu item:
[MenuItem("Assets/DoSomethingWithVariabe")]
private static void DoSomethingWithVariable()
{
// Do something with you variable
}
And this to add a validation :
// Note that we pass the same path, and also pass "true" to the second argument.
[MenuItem("Assets/DoSomethingWithVariabe", true)]
private static bool NewMenuOptionValidation()
{
// This returns true when the selected object is a Variable (the menu item will be disabled otherwise).
return Selection.activeObject is Variable;
}
With this system, you will have an enabled menu item if the asset clicked is of the right type:
While you will get a disabled one if the asset isn't of the right type:
What happen is that when you right click on an asset, the validation method is called and will return true if the clicked asset is of the type you want to have (here a variable), and then, if it returns true, the menu item will be enabled.
Your answer
Follow this Question
Related Questions
Custom Asset Icons? 2 Answers
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
Why Are My Project Assets Constantly Re-Importing? 2 Answers
How to highlight or select an asset in project window from editor script? 2 Answers
Multiple object Selection in project view: Unity shows wrong type 0 Answers