protected virtual GameObject MakePopupGameObject(TextButton templateButton, string selected = null) { // create popup menu GameObject popupGameObject = gameObject.MakeUiObject("Popup"); // create blocker ClickBlocker clickBlocker = ClickBlocker.MakeClickBlocker(popupGameObject, gameObject); // create menu _gameObjectMenu = popupGameObject.MakeUiObject("Menu"); // create background GameObject gameObjectBackground = _gameObjectMenu.MakeUiObject("Background"); Image backgroundImage = gameObjectBackground.AddComponent <Image>(); backgroundImage.sprite = popupBgSprite; float btnWidth = popupTextButton.GetWidth() + padding * 2; float btnHeight = popupTextButton.GetHeight() + padding * 2; float bgHeight = (btnHeight - padding) * _menuItems.Length + padding; backgroundImage.SetSize(btnWidth, bgHeight); backgroundImage.SetY(backgroundImage.GetHeight() / 2 - btnHeight / 2); // set selected, this will move the background Selected = selected; // check if popup in within display region if (!UtilsRect.AinsideB(backgroundImage, clickBlocker)) { // if not, then reverse menuItem order // causing menu to build in the opposite direction Array.Reverse(_menuItems); Selected = selected; } // create selection buttons float yLoc = 0; int counter = 0; _menuButtons = new ClickButton[_menuItems.Length]; foreach (string menuItem in _menuItems) { int index = counter++; TextButton textButton = MakeTextButton(templateButton, menuItem); ClickButton clickButton = textButton.ClickButton; clickButton.SetY(yLoc); clickButton.onClickEvent.AddListener(delegate { MenuButtonClicked(new PopupMenuEventArgs(menuItem, index)); }); clickButton.OnRolloverEvent += delegate { MenuButtonRolled(menuItem, index); }; yLoc += clickButton.GetHeight() + padding; if (index == _selectedIndex) { clickButton.Selected = true; clickButton.Enabled = false; } _menuButtons[index] = clickButton; } return(popupGameObject); }