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?


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:

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:
Doesn't work

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.