概要
using UnityEditor; [InitializeOnLoad] public static class Example { private const string ITEM_NAME = "Tools/Hoge"; static Example() { Menu.SetChecked( ITEM_NAME, true ); } [MenuItem( ITEM_NAME )] private static void Hoge() { } }
InitializeOnLoad のタイミングで自作の MenuItem に対して
Menu.SetChecked を実行すると
Menu Tools/Hoge can't be checked because it doesn't exist
Unity プロジェクト起動時に上記のような警告ログが出力され、
メニューにチェックマークを付けることができない
解決方法
using UnityEditor; [InitializeOnLoad] public static class Example { private const string ITEM_NAME = "Tools/Hoge"; static Example() { EditorApplication.delayCall += () => { Menu.SetChecked( ITEM_NAME, true ); }; } [MenuItem( ITEM_NAME )] private static void Hoge() { } }
1フレーム遅らせて Menu.SetChecked を実行する