Menu Items - Conditional | Editor Scripting | C# | Unity Game Engine


MenuItems_Conditional_Editor.cs
using UnityEngine;
using UnityEditor;
public class MenuItems_Conditional_Editor
{
    [MenuItem("Conditional Menu/Process Material")]
    public static void DoSomethingWithMaterial()
    { }

    //Use the same name in MenuItem and pass 'true' to the second argument to mark the method as Validator.
    //The return type of method is bool and bool value depends on the type of selected object.
    //The menu item will be enabled when the selected object is a material otherwise disabled.
    [MenuItem("Conditional Menu/Process Material", true)]
    public static bool ProcessMaterialValidation()
    {
        return Selection.activeObject.GetType() == typeof(Material);
    }
}