Untiy UGUI ToggleGroup 多按钮互斥 只选中一个按钮并监听按钮点击方法

使用方法:

1.新建一个panel,挂上ToggleGroup组件和Tab组件

2.新建Toggle,看需要多少按钮就新建几个,Toggle里面的Chectmark换成想要点击选中之后的效果

3.获取Tab组件,在开始方法中注册监听事件,RankTab.AddSelectedListener(SelectRankType)

4.在监听事件中能根据toggleName识别点击的是哪个Toggle

private void SelectRankType(string toggleName,int index)
        {
            if(toggleName== "FullSeverRank")
            {
                //请求全服排行榜
                Debug.Log("点击全服排行榜");
                isFullRank = true;
            }
            else if(toggleName == "FriendsRank")
            {
                //请求好友排行榜
                Debug.Log("点击好友排行榜");
                isFullRank = false;
                GameEntry.HttpConnector.GetFriendsRank(RankInterfaceApi.UsetId,  friendsPage, limit, FriendsResponseCallback);
            }
            else
            {
                Debug.LogWarning("没有这个toogle");
            }
        }

Tab组件的代码:

using System;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.Events;
namespace ArrowLegend
{
    /// 
    /// tabUI组件实现ToggleGroup效果
    /// 
    [RequireComponent(typeof(ToggleGroup))]
    public class Tab : MonoBehaviour
    {
        
        private ToggleGroup toggleGroup;
        private List toggles;
        private Toggle currentSelectToggle;
        private int currentSelectToggleIndex;
        private List> selectedListener;
        private List> unselectedListener;
        private bool isInit = false;
        private void Awake()
        {
            Init();
        }
        public void Init()
        {
            if (isInit)
            {
                return;
            }
            isInit = true;
            selectedListener = new List>(5);
            unselectedListener = new List>(5);
            toggles = new List();
            toggleGroup = GetComponent();
            GetComponentsInChildren(true, toggles);
            for (int i = 0; i < toggles.Count; i++)
            {
                Toggle toggle = toggles[i];
                toggle.group = toggleGroup;
                toggle.onValueChanged.AddListener(OnTabChange);
                if (toggle.isOn)
                {
                    if (currentSelectToggle == null)
                    {
                        currentSelectToggle = toggle;
                    }
                    else
                    {
                        toggle.isOn = false;
                    }
                }
            }
        }
        public string GetSelectedName()
        {
            if (currentSelectToggle != null)
            {
                return currentSelectToggle.name;
            }
            return null;
        }
        public void SetSelectedName(string toggleName)
        {
            for (int i = 0; i < toggles.Count; i++)
            {
                if (toggles[i].name == toggleName)
                {
                    if (!toggles[i].isOn)
                    {
                        toggles[i].isOn = true;
                    }
                    break;
                }
            }
        }
        public int GetSelectedIndex()
        {
            if (currentSelectToggle != null)
            {
                return toggles.IndexOf(currentSelectToggle);
            }
            return -1;
        }
        public void SetSelectedIndex(int index)
        {
            if (index >= 0 && index < toggles.Count)
            {
                toggles[index].isOn = true;
            }
        }
        public int AddToggle(Toggle toggle)
        {
          
            if (toggle != null)
            {
                bool isExist = toggles.Contains(toggle);
            
                if (!isExist)
                {
                    toggle.isOn = false;
                    toggle.group = toggleGroup;
                    toggles.Add(toggle);
                    return toggles.Count;
                }
            }
            return -1;
        }
        public void RemoveToggle(Toggle toggle)
        {
            if (toggle != null)
            {
                int index = toggles.IndexOf(toggle);
                if (index >= 0)
                {
                    toggles.RemoveAt(index);
                    if (currentSelectToggle == toggle)
                    {
                        currentSelectToggle = null;
                        currentSelectToggleIndex = -1;
                        if (toggles.Count > 0)
                        {
                            currentSelectToggle = toggles[0];
                            currentSelectToggle.isOn = true;
                            currentSelectToggleIndex = 0;
                        }
                    }
                    else
                    {
                        for (int i = index; i < toggles.Count; i++)
                        {
                            if (currentSelectToggle == toggles[i])
                            {
                                currentSelectToggleIndex = i;
                                break;
                            }
                        }
                    }
                }
            }
        }
        public void AddSelectedListener(Action listener)
        {
            Init();
            selectedListener.Add(listener);
        }
        public void AddUnselectedListener(Action listener)
        {
            unselectedListener.Add(listener);
        }
        public void RemoveSelectedListener(Action listener)
        {
            selectedListener.Remove(listener);
        }
        public void RemoveAllSelectedListeners()
        {
            selectedListener.Clear();
        }
        public void RemoveUnselectedListener(Action listener)
        {
            unselectedListener.Remove(listener);
        }
        public void RemoveAllUnselectedListeners()
        {
            unselectedListener.Clear();
        }
        private void OnTabChange(bool isSelect)
        {
            if (isSelect)
            {
                for (int i = 0; i < toggles.Count; i++)
                {
                    Toggle toggle = toggles[i];
                    if (toggle.isOn)
                    {
                        if (toggle == currentSelectToggle)
                        {
                            return;
                        }
                        if (currentSelectToggle != null)
                        {
                            CallUnselectedListener(currentSelectToggle,  currentSelectToggleIndex + 1);
                        }
                        currentSelectToggle = toggle;
                        currentSelectToggleIndex = i;
                        CallSelectedListener(currentSelectToggle, currentSelectToggleIndex  + 1);
                        break;
                    }
                }
            }
        }
        private void CallSelectedListener(Toggle toggle, int index)
        {
            for (int i = selectedListener.Count - 1; i >= 0; i--)
            {
                selectedListener[i]?.Invoke(toggle.name, index);
            }
        }
        private void CallUnselectedListener(Toggle toggle, int index)
        {
            for (int i = unselectedListener.Count - 1; i >= 0; i--)
            {
                unselectedListener[i]?.Invoke(toggle.name, index);
            }
        }
        private void OnDestroy()
        {
            RemoveAllSelectedListeners();
            RemoveAllUnselectedListeners();
            toggles.Clear();
            selectedListener = null;
        }
    }
}

 

你可能感兴趣的:(项目笔记)