コード例 #1
0
        private void CreatePopup(int count, MCMenu parent)
        {
            // Get the Menu object, we need this because we have a button that closes the menu.
            MCMenu popupMenu = MenuController.GetMenuGlobal("SIMPLEPOPUP");

            // Create the data for the menu.
            MCSimplePopupData simplePopupData = new MCSimplePopupData("title " + count, "This is another popup.",
                                                                      new MCButtonData[]
            {
                new MCButtonData("New popup", button => { CreatePopup(count + 1, popupMenu); }, null, true, "Creates a new popup"),
                new MCButtonData("Tooltip", null, null, true, "Simply shows the tooltip working"),
                new MCButtonData("Close parent", button => { MenuController.HideMenuGlobal(popupMenu.Parent); }, null, true, "Closes the parent menu (which will close all children)"),
                new MCButtonData("Close this", button => popupMenu.Hide(), null, true, "Closes this popup")
            });

            if (parent)
            {
                parent.AddPopup(popupMenu, simplePopupData);
            }
            else
            {
                // Add the popup to the screen, when there is nothing on the screen it will be added as a menu instead of a popup.
                MenuController.AddPopupGlobal(popupMenu, true, simplePopupData);

                // In case you have a specific menucontroller that you want to use:
                // yourMenuController.AddPopup(popupMenu, true, simplePopupData);
            }
        }
コード例 #2
0
        public override void Show(object data)
        {
            if (data == null)
            {
                throw new Exception("Trying to start a simple popup with a data that is equal to null!");
            }

            base.Show(data);

            MCSimplePopupData simplePopupData = data as MCSimplePopupData;

            if (simplePopupData == null)
            {
                throw new Exception("Trying to show a simple popup, but using the wrong data type: " + data.GetType());
            }

            bool hasButtons = simplePopupData.ButtonDatas != null && simplePopupData.ButtonDatas.Length > 0;

            _disableIfNoButtons.SetActive(hasButtons);
            for (int i = 0; i < simplePopupData.ButtonDatas.Length; i++)
            {
                Button newButton = Instantiate(_buttonPrefab, _buttonParent);
                newButton.onClick.RemoveAllListeners();
                MCButtonData buttonData = simplePopupData.ButtonDatas[i];
                if (buttonData.ButtonClick != null)
                {
                    newButton.onClick.AddListener(() => buttonData.ButtonClick(newButton));
                }

                // Use the icon if one is present, use text otherwise.
                if (buttonData.Icon != null)
                {
                    Image imageChild = newButton.GetComponentInChildren <Image>();
                    imageChild.sprite = buttonData.Icon;
                }
                else
                {
                    Text textChild = newButton.GetComponentInChildren <Text>();
                    textChild.text = buttonData.Text;
                }

                // Create the tooltip component and add the OnHover components to the object if it wants a tooltip.
                if (buttonData.Tooltip)
                {
                    MCSimpleTooltipData simpleTooltipData = new MCSimpleTooltipData("Tooltip", buttonData.TooltipText,
                                                                                    newButton.GetComponent <RectTransform>())
                    {
                        AutoRemove = true
                    };

                    OnHover onHover = newButton.GetComponent <OnHover>();
                    if (!onHover)
                    {
                        onHover = newButton.gameObject.AddComponent <OnHover>();
                    }
                    onHover.Delay           = 1;
                    onHover.onPointerDelay += () => {
                        this.MenuController.AddPopup("SIMPLETOOLTIP", false, simpleTooltipData);
                    };
                }

                newButton.gameObject.SetActive(true);
            }

            _buttonPrefab.gameObject.SetActive(false);

            gameObject.SetActive(true);
        }