Beispiel #1
0
    /// <summary>
    /// 更新所有分类
    /// </summary>
    private void UpdateList()
    {
        Transform ButtonBox = FindComponent <Transform>("Content/ButtonBox");
        Transform template  = FindComponent <Transform>("Content/ButtonBox/Button");

        if (!ButtonBox || !template)
        {
            return;
        }

        UIViewCategory[] categorys = State.GetPage().Categorys;

        int index = 0;
        int count = categorys != null ? categorys.Length : 0;

        for (index = 0; index < count; index++)
        {
            Transform child = null;
            if (index < ButtonBox.childCount)
            {
                child = ButtonBox.GetChild(index);
            }
            else
            {
                child = UnityEngine.Object.Instantiate(template, ButtonBox);
            }

            child.gameObject.SetActive(true);

            //更新显示
            UIIconAndLabel buttonInfo = child.GetComponent <UIIconAndLabel>();
            if (buttonInfo)
            {
                if (buttonInfo.Label)
                {
                    buttonInfo.Label.text = categorys[index].Label;
                }

                if (buttonInfo.Icon)
                {
                    buttonInfo.Icon.sprite = null;
                }
            }

            //绑定事件
            Toggle button = child.GetComponent <Toggle>();
            if (button)
            {
                int id = index;
                button.navigation = new Navigation()
                {
                    mode = Navigation.Mode.None
                };
                button.onValueChanged.RemoveAllListeners();
                button.onValueChanged.AddListener((selected) =>
                {
                    if (button.animator != null)
                    {
                        button.animator.SetBool("IsOn", selected);
                        button.animator.SetTrigger("Normal");
                    }

                    if (selected)
                    {
                        if (OwnerView != null)
                        {
                            ChangeFilterIndex(id);
                        }
                    }
                });
            }

            //音效处理
            ClickSoundListener handler = child.GetComponent <ClickSoundListener>();
            if (!handler)
            {
                handler = child.GetOrAddComponent <ClickSoundListener>();
            }
        }

        //关闭多余的按钮
        for (; index < ButtonBox.childCount; index++)
        {
            Transform child = ButtonBox.GetChild(index);
            child.gameObject.SetActive(false);

            Toggle button = child.GetComponent <Toggle>();
            if (button)
            {
                button.onValueChanged.RemoveAllListeners();
                button.isOn = false;
            }
        }
    }
Beispiel #2
0
    /// <summary>
    /// 更新列表
    /// </summary>
    private void UpdateList()
    {
        ClearList();

        Transform ButtonBox = FindComponent <Transform>("Content/ButtonBox");
        Transform template  = FindComponent <Transform>("Content/ButtonBox/Button");

        if (ButtonBox && template)
        {
            CfgEternityProxy cfg = GameFacade.Instance.RetrieveProxy(ProxyName.CfgEternityProxy) as CfgEternityProxy;

            int index = 0;
            int count = GetPageCount();

            for (index = 0; index < count; index++)
            {
                //数据
                UiLabelConfig?pageCfg = cfg.GetUIPage((uint)Config.Value.LabelId(index));

                //视图
                Transform child = null;
                if (index < ButtonBox.childCount)
                {
                    child = ButtonBox.GetChild(index);
                }
                else
                {
                    child = Object.Instantiate(template, ButtonBox);
                }

                //更新显示
                child.gameObject.SetActive(OwnerView.State.GetPageVisible(index));

                UIIconAndLabel buttonInfo = child.GetComponent <UIIconAndLabel>();
                if (buttonInfo)
                {
                    if (buttonInfo.Icon)
                    {
                        if (pageCfg.HasValue && pageCfg.Value.Icon != 0)
                        {
                            UIUtil.SetIconImage(buttonInfo.Icon, (uint)pageCfg.Value.Icon);
                        }
                        else
                        {
                            buttonInfo.Icon.sprite = null;
                        }
                    }

                    if (buttonInfo.Label)
                    {
                        buttonInfo.Label.text = pageCfg.HasValue ? GetLocalization(pageCfg.Value.Name) : string.Empty;
                    }

                    if (buttonInfo.Info)
                    {
                        buttonInfo.Info.text = OwnerView.State.GetPageLabel(index);
                    }
                }

                //绑定事件
                Toggle button = child.GetComponent <Toggle>();
                if (button)
                {
                    int id = index;
                    button.navigation = new Navigation()
                    {
                        mode = Navigation.Mode.None
                    };
                    button.onValueChanged.RemoveAllListeners();
                    button.onValueChanged.AddListener((selected) =>
                    {
                        if (button.animator != null)
                        {
                            button.animator.SetBool("IsOn", selected);
                            button.animator.SetTrigger("Normal");
                        }

                        if (selected && !m_SkipEventListener)
                        {
                            if (OwnerView != null)
                            {
                                ChangePageIndex(id);
                            }
                        }
                    });
                }

                //音效处理
                ClickSoundListener handler = child.GetComponent <ClickSoundListener>();
                if (!handler)
                {
                    handler = child.GetOrAddComponent <ClickSoundListener>();
                }
            }

            //关闭多余的按钮
            for (; index < ButtonBox.childCount; index++)
            {
                Transform child = ButtonBox.GetChild(index);
                child.gameObject.SetActive(false);

                Toggle button = child.GetComponent <Toggle>();
                if (button)
                {
                    button.onValueChanged.RemoveAllListeners();
                    button.isOn = false;
                }
            }
        }
    }