コード例 #1
0
        internal static void BuildWindowMenuListing()
        {
            const string k_RootMenuItemName = "Window/Panels";

            Menu.RemoveMenuItem(k_RootMenuItemName);
            var editorWindows = Resources.FindObjectsOfTypeAll <EditorWindow>();
            int menuIdx       = -10;

            Menu.AddMenuItem($"{k_RootMenuItemName}/Close all floating panels...", "", false, menuIdx++, () =>
            {
                var windows = Resources.FindObjectsOfTypeAll <ContainerWindow>();
                foreach (var win in windows.Where(w => !!w && w.showMode != ShowMode.MainWindow))
                {
                    win.Close();
                }
            }, null);
            Menu.AddSeparator($"{k_RootMenuItemName}/", menuIdx++);

            int menuIndex = 1;

            foreach (var win in editorWindows.Where(e => !!e).OrderBy(e => e.titleContent.text))
            {
                var title = win.titleContent.text;
                if (!String.IsNullOrEmpty(win.titleContent.tooltip) && win.titleContent.tooltip != title)
                {
                    title = win.titleContent.tooltip;
                }
                title = title.Replace("/", "\\");
                Menu.AddMenuItem($"{k_RootMenuItemName}/{menuIndex++} {title}", "", false, menuIdx++, () => win.Focus(), null);
            }
            EditorUtility.Internal_UpdateAllMenus();
        }
コード例 #2
0
        internal static void ReloadWindowLayoutMenu()
        {
            Menu.RemoveMenuItem("Window/Layouts");

            int layoutMenuItemPriority = 20;

            // Get user saved layouts
            if (Directory.Exists(layoutsModePreferencesPath))
            {
                var layoutPaths = Directory.GetFiles(layoutsModePreferencesPath).Where(path => path.EndsWith(".wlt")).ToArray();
                foreach (var layoutPath in layoutPaths)
                {
                    var name = Path.GetFileNameWithoutExtension(layoutPath);
                    Menu.AddMenuItem("Window/Layouts/" + name, "", false, layoutMenuItemPriority++, () => LoadWindowLayout(layoutPath, false), null);
                }

                layoutMenuItemPriority += 500;
            }

            // Get mode layouts
            var modeLayoutPaths = ModeService.GetModeDataSection(ModeService.currentIndex, ModeService.k_LayoutsSectionName) as IList <object>;

            if (modeLayoutPaths != null)
            {
                foreach (var layoutPath in modeLayoutPaths.Cast <string>())
                {
                    if (!File.Exists(layoutPath))
                    {
                        continue;
                    }
                    var name = Path.GetFileNameWithoutExtension(layoutPath);
                    Menu.AddMenuItem("Window/Layouts/" + name, "", Toolbar.lastLoadedLayoutName == name, layoutMenuItemPriority++, () => LoadWindowLayout(layoutPath, false), null);
                }
            }

            layoutMenuItemPriority += 500;

            Menu.AddMenuItem("Window/Layouts/Save Layout...", "", false, layoutMenuItemPriority++, SaveGUI, null);
            Menu.AddMenuItem("Window/Layouts/Delete Layout...", "", false, layoutMenuItemPriority++, DeleteGUI, null);
            Menu.AddMenuItem("Window/Layouts/Revert Factory Settings...", "", false, layoutMenuItemPriority++, () => RevertFactorySettings(false), null);
        }
コード例 #3
0
ファイル: ModeService.cs プロジェクト: zvars/UnityCsReference
        private static void LoadMenu(IList menus, string prefix = "", int priority = 100)
        {
            const string k_MenuKeyName              = "name";
            const string k_MenuKeyItemId            = "menu_item_id";
            const string k_MenuKeyCommandId         = "command_id";
            const string k_MenuKeyValidateCommandId = "validate_command_id";
            const string k_MenuKeyChildren          = "children";
            const string k_MenuKeyPriority          = "priority";
            const string k_MenuKeyInternal          = "internal";
            const string k_MenuKeyShortcut          = "shortcut";
            const string k_MenuKeyChecked           = "checked";
            const string k_MenuKeyPlatform          = "platform";
            const string k_MenuKeyRename            = "rename";

            if (menus == null)
            {
                return;
            }

            foreach (var menuData in menus)
            {
                if (menuData != null)
                {
                    var menu = menuData as JSONObject;
                    if (menu == null)
                    {
                        continue;
                    }
                    var isInternal = JsonUtils.JsonReadBoolean(menu, k_MenuKeyInternal);
                    if (isInternal && !Unsupported.IsDeveloperMode())
                    {
                        continue;
                    }
                    var menuName            = JsonUtils.JsonReadString(menu, k_MenuKeyName);
                    var fullMenuName        = prefix + menuName;
                    var platform            = JsonUtils.JsonReadString(menu, k_MenuKeyPlatform);
                    var hasExplicitPriority = menu.Contains(k_MenuKeyPriority);
                    priority = JsonUtils.JsonReadInt(menu, k_MenuKeyPriority, priority + 1);

                    // Check the menu item platform
                    if (!String.IsNullOrEmpty(platform) && !Application.platform.ToString().ToLowerInvariant().StartsWith(platform.ToLowerInvariant()))
                    {
                        continue;
                    }

                    // Check if we are a submenu
                    if (menu.Contains(k_MenuKeyChildren))
                    {
                        if (menu[k_MenuKeyChildren] is IList)
                        {
                            LoadMenu(menu[k_MenuKeyChildren] as IList, fullMenuName + "/", priority);
                        }
                        else if (menu[k_MenuKeyChildren] is string && (string)menu[k_MenuKeyChildren] == "*")
                        {
                            var whitelistedItems = Menu.ExtractSubmenus(fullMenuName);
                            var renamedTo        = prefix + JsonUtils.JsonReadString(menu, k_MenuKeyRename, menuName);
                            foreach (var wi in whitelistedItems)
                            {
                                Menu.AddExistingMenuItem(wi.Replace(fullMenuName, renamedTo), wi, hasExplicitPriority ? priority : -1);
                            }
                        }
                    }
                    else
                    {
                        var commandId = JsonUtils.JsonReadString(menu, k_MenuKeyCommandId);
                        if (String.IsNullOrEmpty(commandId))
                        {
                            // We are re-using a default menu item
                            var menuItemId = JsonUtils.JsonReadString(menu, k_MenuKeyItemId, fullMenuName);
                            if (fullMenuName.Contains('/'))
                            {
                                Menu.AddExistingMenuItem(fullMenuName, menuItemId, priority);
                            }
                        }
                        else if (CommandService.Exists(commandId))
                        {
                            // Create a new menu item pointing to a command handler
                            var shortcut = JsonUtils.JsonReadString(menu, k_MenuKeyShortcut);
                            var @checked = JsonUtils.JsonReadBoolean(menu, k_MenuKeyChecked);

                            Func <bool> validateHandler   = null;
                            var         validateCommandId = JsonUtils.JsonReadString(menu, k_MenuKeyValidateCommandId);
                            if (!String.IsNullOrEmpty(validateCommandId))
                            {
                                validateHandler = () => (bool)CommandService.Execute(validateCommandId, CommandHint.Menu | CommandHint.Validate);
                            }

                            Menu.AddMenuItem(fullMenuName, shortcut, @checked, priority, () => CommandService.Execute(commandId, CommandHint.Menu), validateHandler);
                        }
                    }
                }
                else
                {
                    priority += 100;
                }
            }
        }