使用方法:
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
{
/// <summary>
/// tabUI组件实现ToggleGroup效果
/// </summary>
[RequireComponent(typeof(ToggleGroup))]
public class Tab : MonoBehaviour
{
private ToggleGroup toggleGroup;
private List<Toggle> toggles;
private Toggle currentSelectToggle;
private int currentSelectToggleIndex;
private List<Action<string, int>> selectedListener;
private