Ejemplo n.º 1
0
        // Find menus from command id
        private static void LoadMenuFromCommandId(IList menus, Dictionary <string, MenuItemScriptCommand> menuItems, string prefix = "")
        {
            if (menus == null || menuItems == 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);

                    // 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 children)
                        {
                            LoadMenuFromCommandId(children, menuItems, fullMenuName + "/");
                        }
                    }
                    else
                    {
                        var commandId = JsonUtils.JsonReadString(menu, k_MenuKeyCommandId);
                        if (!string.IsNullOrEmpty(commandId) && 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);

                            var validateCommandId = JsonUtils.JsonReadString(menu, k_MenuKeyValidateCommandId);
                            var commandMenuItem   = MenuItemScriptCommand.InitializeFromCommand(fullMenuName, 100, commandId, validateCommandId);
                            commandMenuItem.@checked = @checked;
                            commandMenuItem.shortcut = shortcut;
                            menuItems[fullMenuName]  = commandMenuItem;
                        }
                    }
                }
            }
        }