How to add right click context menu to /assets, which display it only for one file type?

I want to add context menu when hitting right mouse button on the material file in project browser, but I don’t want to display it when hitting right mouse button in other file type than Material.
So, I want to call something like this:

[MenuItem("Assets/Open in Material Edit")]
            private static void OpenMaterialEdit(MenuCommand menuCommand)
            {
                for (int i = 0; i < Selection.objects.Length; i++)
                {
                    if (Selection.objects[i] is Material)
                    {
                        if (Get == null) Init();
                        Get.TargetMaterial = Selection.objects[i] as Material;
                        return;
                    }
                }
            }

But now it will be displayed on every file in project, when I want to have this option visible only for Material type.
There is something like: [MenuItem(“CONTEXT/Material/Open in Material Edit”)]
But it’s for hitting right mouse button on the added component inside inspector window, maybe there is something similar for assets file menu item?

The best you can do is to disable the menu item, I think. Read this, look for “validate”. It probably works.

1 Like

It only “disables” the menu item, it’s still visible in menu list, I want to hide it completely.

That’s what I just said, yes.

AFAIK, you can’t. If you could, you could find it on the docs page I linked. Unity user menus are generated on code compile, because they are attributes. You don’t want to recompile every time you select or deselect an asset. I think you should settle for enable/disable.

1 Like