UITabSwitcher.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System.Collections.Generic;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace AppUI.Util.TabSwitcher
  6. {
  7. public class UITabSwitcher : MonoBehaviour
  8. {
  9. [System.Serializable]
  10. public class TabItem
  11. {
  12. public string tabId;
  13. [Header("按钮")]
  14. public GameObject btn;
  15. [Header("标题")]
  16. public TMP_Text label;
  17. [Header("内容页")]
  18. public GameObject panel;
  19. [Header("默认允许显示")]
  20. public bool defaultVisible = true;
  21. }
  22. [SerializeField] List<TabItem> tabs = new();
  23. [Header("默认Tab")]
  24. [SerializeField] string defaultTab;
  25. [Header("文字颜色")]
  26. [SerializeField] Color selectColor = Color.white;
  27. [SerializeField]
  28. Color normalColor = new Color32(153, 153, 153, 255);
  29. Dictionary<string, TabItem> tabMap = new();
  30. void Awake()
  31. {
  32. Init();
  33. }
  34. public void SetDefaultTab(string tabId)
  35. {
  36. defaultTab = tabId;
  37. }
  38. void Init()
  39. {
  40. tabMap.Clear();
  41. foreach (var tab in tabs)
  42. {
  43. if (tab == null || string.IsNullOrEmpty(tab.tabId))
  44. continue;
  45. tabMap[tab.tabId] = tab;
  46. if (tab.btn != null)
  47. {
  48. tab.btn.SetActive(tab.defaultVisible);
  49. // 自动绑定按钮事件
  50. Button btn = tab.btn.GetComponent<Button>();
  51. if (btn != null)
  52. {
  53. btn.onClick.RemoveAllListeners();
  54. string cacheTabId = tab.tabId;
  55. btn.onClick.AddListener(() =>
  56. {
  57. OnClick_Tab(cacheTabId);
  58. });
  59. }
  60. }
  61. }
  62. RefreshDefaultTab();
  63. }
  64. void RefreshDefaultTab()
  65. {
  66. if (string.IsNullOrEmpty(defaultTab))
  67. return;
  68. if (tabMap.TryGetValue(defaultTab, out var tab))
  69. {
  70. if (tab.btn != null && tab.btn.activeSelf)
  71. {
  72. OnClick_Tab(tab.tabId);
  73. }
  74. }
  75. }
  76. // ======================================
  77. // 点击切换
  78. // ======================================
  79. public void OnClick_Tab(string tabId)
  80. {
  81. foreach (var kv in tabMap)
  82. {
  83. bool selected = kv.Key == tabId;
  84. var tab = kv.Value;
  85. if (tab.panel != null)
  86. {
  87. tab.panel.SetActive(selected);
  88. }
  89. if (tab.label != null)
  90. {
  91. tab.label.color = selected
  92. ? selectColor
  93. : normalColor;
  94. }
  95. }
  96. AudioMgr.ins?.PlayBtn();
  97. }
  98. // ======================================
  99. // 控制Tab显示
  100. // ======================================
  101. public void SetTabVisible(string tabId, bool visible)
  102. {
  103. if (!tabMap.TryGetValue(tabId, out var tab))
  104. return;
  105. if (tab.btn != null)
  106. {
  107. tab.btn.SetActive(visible);
  108. }
  109. // 如果当前页被隐藏
  110. // 自动切换到第一个可见Tab
  111. if (!visible)
  112. {
  113. TrySelectFirstVisibleTab();
  114. }
  115. }
  116. void TrySelectFirstVisibleTab()
  117. {
  118. foreach (var tab in tabMap.Values)
  119. {
  120. if (tab.btn != null && tab.btn.activeSelf)
  121. {
  122. OnClick_Tab(tab.tabId);
  123. return;
  124. }
  125. }
  126. }
  127. // ======================================
  128. // 批量设置
  129. // ======================================
  130. public void SetVisibleTabs(params string[] allowTabs)
  131. {
  132. HashSet<string> set = new HashSet<string>(allowTabs);
  133. foreach (var kv in tabMap)
  134. {
  135. bool visible = set.Contains(kv.Key);
  136. if (kv.Value.btn != null)
  137. {
  138. kv.Value.btn.SetActive(visible);
  139. }
  140. }
  141. TrySelectFirstVisibleTab();
  142. }
  143. }
  144. }