| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- namespace AppUI.Util.TabSwitcher
- {
- public class UITabSwitcher : MonoBehaviour
- {
- [System.Serializable]
- public class TabItem
- {
- public string tabId;
- [Header("按钮")]
- public GameObject btn;
- [Header("标题")]
- public TMP_Text label;
- [Header("内容页")]
- public GameObject panel;
- [Header("默认允许显示")]
- public bool defaultVisible = true;
- }
- [SerializeField] List<TabItem> tabs = new();
- [Header("默认Tab")]
- [SerializeField] string defaultTab;
- [Header("文字颜色")]
- [SerializeField] Color selectColor = Color.white;
- [SerializeField]
- Color normalColor = new Color32(153, 153, 153, 255);
- Dictionary<string, TabItem> tabMap = new();
- void Awake()
- {
- Init();
- }
- public void SetDefaultTab(string tabId)
- {
- defaultTab = tabId;
- }
- void Init()
- {
- tabMap.Clear();
- foreach (var tab in tabs)
- {
- if (tab == null || string.IsNullOrEmpty(tab.tabId))
- continue;
- tabMap[tab.tabId] = tab;
- if (tab.btn != null)
- {
- tab.btn.SetActive(tab.defaultVisible);
- // 自动绑定按钮事件
- Button btn = tab.btn.GetComponent<Button>();
- if (btn != null)
- {
- btn.onClick.RemoveAllListeners();
- string cacheTabId = tab.tabId;
- btn.onClick.AddListener(() =>
- {
- OnClick_Tab(cacheTabId);
- });
- }
- }
- }
- RefreshDefaultTab();
- }
- void RefreshDefaultTab()
- {
- if (string.IsNullOrEmpty(defaultTab))
- return;
- if (tabMap.TryGetValue(defaultTab, out var tab))
- {
- if (tab.btn != null && tab.btn.activeSelf)
- {
- OnClick_Tab(tab.tabId);
- }
- }
- }
- // ======================================
- // 点击切换
- // ======================================
- public void OnClick_Tab(string tabId)
- {
- foreach (var kv in tabMap)
- {
- bool selected = kv.Key == tabId;
- var tab = kv.Value;
- if (tab.panel != null)
- {
- tab.panel.SetActive(selected);
- }
- if (tab.label != null)
- {
- tab.label.color = selected
- ? selectColor
- : normalColor;
- }
- }
- AudioMgr.ins?.PlayBtn();
- }
- // ======================================
- // 控制Tab显示
- // ======================================
- public void SetTabVisible(string tabId, bool visible)
- {
- if (!tabMap.TryGetValue(tabId, out var tab))
- return;
- if (tab.btn != null)
- {
- tab.btn.SetActive(visible);
- }
- // 如果当前页被隐藏
- // 自动切换到第一个可见Tab
- if (!visible)
- {
- TrySelectFirstVisibleTab();
- }
- }
- void TrySelectFirstVisibleTab()
- {
- foreach (var tab in tabMap.Values)
- {
- if (tab.btn != null && tab.btn.activeSelf)
- {
- OnClick_Tab(tab.tabId);
- return;
- }
- }
- }
- // ======================================
- // 批量设置
- // ======================================
- public void SetVisibleTabs(params string[] allowTabs)
- {
- HashSet<string> set = new HashSet<string>(allowTabs);
- foreach (var kv in tabMap)
- {
- bool visible = set.Contains(kv.Key);
- if (kv.Value.btn != null)
- {
- kv.Value.btn.SetActive(visible);
- }
- }
- TrySelectFirstVisibleTab();
- }
- }
- }
|