Exemple #1
0
        public void Redraw()
        {
            MenuDefinition menuDefinition = _currentMenu.BuildMenu(this);

            Texture2D texture = CacheManager.Instance.GetTexture(menuDefinition.BackgroundFilename);

            Background.texture = texture;
            Background.rectTransform.sizeDelta = new Vector2(texture.width, texture.height);

            int selectedIndex = EventSystem.current.currentSelectedGameObject == null ? 0 : EventSystem.current.currentSelectedGameObject.transform.GetSiblingIndex();

            foreach (Transform child in Items.transform)
            {
                Destroy(child.gameObject);
            }

            for (int i = 0; i < menuDefinition.MenuItems.Length; i++)
            {
                MenuItem menuItem = menuDefinition.MenuItems[i];
                if (menuItem is MenuButton)
                {
                    MenuButton menuButton = menuItem as MenuButton;
                    Button     button     = Instantiate(MenuButtonPrefab, Items.transform);
                    button.transform.Find("TextContainer").GetComponentInChildren <Text>().text = menuButton.Text;
                    button.transform.Find("Value").GetComponent <Text>().text = menuButton.Value;
                    button.onClick.AddListener(new UnityEngine.Events.UnityAction(menuButton.OnClick));

                    if (selectedIndex == i)
                    {
                        EventSystem.current.SetSelectedGameObject(button.gameObject);
                    }
                }
                else if (menuItem is MenuBlank)
                {
                    Instantiate(BlankSeparatorPrefab, Items.transform);
                }
            }
        }