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


MenuItems_Conditional_Editor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
    }
}