static void AppendMenuItem(StringBuilder sb, MenuActionData data) { var category = GetActionCategory(data.path); var priority = GetMenuPriority(category); var menuItemShortcut = GetMenuFormattedShortcut(data.menuItemShortcut); // Verify sb.AppendLine($"\t\t[MenuItem(k_MenuPrefix + \"{data.path}{menuItemShortcut}\", true, {priority})]"); sb.AppendLine($"\t\tstatic bool MenuVerify_{data.typeString}()"); sb.AppendLine("\t\t{"); sb.AppendLine($"\t\t\tvar instance = EditorToolbarLoader.GetInstance<{data.typeString}>();"); sb.AppendLine("\t\t\treturn instance != null && instance.enabled;"); sb.AppendLine("\t\t}"); sb.AppendLine(); #if SHORTCUT_MANAGER var shortcutInfo = data.type.GetCustomAttribute <MenuActionShortcutAttribute>(); if (shortcutInfo != null) { sb.AppendLine("#if SHORTCUT_MANAGER"); var key = GetShortcutAttributeKeyBindingArgs(shortcutInfo.key, shortcutInfo.modifiers); var ctx = shortcutInfo.context == null ? "null" : $"typeof({shortcutInfo.context})"; if (!string.IsNullOrEmpty(key)) { sb.AppendLine($"\t\t[Shortcut(k_ShortcutPrefix + \"{data.path}\", {ctx}, {key})]"); } else { sb.AppendLine($"\t\t[Shortcut(k_ShortcutPrefix + \"{data.path}\", {ctx})]"); } sb.AppendLine("#endif"); } #endif // Action sb.AppendLine($"\t\t[MenuItem(k_MenuPrefix + \"{data.path}{menuItemShortcut}\", false, {priority})]"); sb.AppendLine($"\t\tstatic void MenuPerform_{data.typeString}()"); sb.AppendLine("\t\t{"); sb.AppendLine($"\t\t\tvar instance = EditorToolbarLoader.GetInstance<{data.typeString}>();"); // *Important* The `instance.enabled` check is redundant for MenuItems, but not for ShortcutManager // shortcuts, which atm only have the context of what EditorWindow is active. sb.AppendLine("\t\t\tif(instance != null && instance.enabled)"); sb.AppendLine("\t\t\t\tEditorUtility.ShowNotification(instance.DoAction().notification);"); sb.AppendLine("\t\t}"); }
static void GenerateMenuItemsForActions() { if (File.Exists(k_GeneratedFilePath)) { File.Delete(k_GeneratedFilePath); } StringBuilder sb = new StringBuilder(); AppendHeader(sb); IEnumerable <string> actions = Directory.GetFiles(k_MenuActionsFolder, "*.cs", SearchOption.AllDirectories) .Select(x => x.Replace("\\", "/")) .Where(y => !IgnoreActions.Contains(GetClassName(y))); foreach (string action in actions) { var data = new MenuActionData(action); if (!data.visibleInMenu) { continue; } if (data.valid) { sb.AppendLine(); AppendMenuItem(sb, data); } } sb.AppendLine("\t}"); sb.AppendLine("}"); File.WriteAllText(k_GeneratedFilePath, sb.ToString().Replace("\r\n", "\n")); EditorUtility.ShowNotification("Successfully Generated\nMenu Items"); AssetDatabase.Refresh(); }