using Assets.MyGame.Scripts.Net; using Assets.MyGame.Scripts.Net.API.GameConfig_SubClass; using Assets.MyGame.Scripts.Net.WebResponses; using Newtonsoft.Json; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class UIHome_BY_MySelectRoomPanel : SubUIBase { public GameObject panel_PanelParent; public Button panel_RoomItemPrefab; public Button btn_NextPage; public Button btn_UpPage; public TextMeshProUGUI text_PageNum; public ScrollRect scroll; public List itemPage = new List(); public GameObject itemPagePrefab; public Transform itemPageParent; public Transform tra_DaQuParent; public Transform tra_FenQuItem; // child1=场次名, child2=分区n public Dictionary itemListDic = new Dictionary(); int pageNum = 1; float horizontalNormalizedPosition; private int _lastLeave = -1; public override void Awake() { btn_NextPage.onClick.AddListener(() => { if (pageNum > 1) pageNum--; text_PageNum.text = pageNum + "/" + itemPage.Count; horizontalNormalizedPosition = (float)(pageNum - 1) / (float)(itemPage.Count - 1); }); btn_UpPage.onClick.AddListener(() => { if (pageNum < itemPage.Count) pageNum++; text_PageNum.text = pageNum + "/" + itemPage.Count; horizontalNormalizedPosition = (float)(pageNum - 1) / (float)(itemPage.Count - 1); }); btn_Close.onClick.AddListener(() => Hide()); } private void Update() { scroll.horizontalNormalizedPosition = Mathf.Lerp(scroll.horizontalNormalizedPosition, horizontalNormalizedPosition, 0.5f); } public override void Hide() { base.Hide(); panel_PanelParent.SetActive(true); } protected override void OnShow(object args = null) { base.OnShow(args); int leave = GetCurrentLeave(); if (_lastLeave != leave) { // 场次变了 → 重建分区 Tab for (int i = tra_DaQuParent.childCount - 1; i >= 0; i--) Destroy(tra_DaQuParent.GetChild(i).gameObject); BuildFenQuTabs(); _lastLeave = leave; } } public override void Show(object args = null) { base.Show(args); ClearRoomPages(); if (args != null) { var v = JsonConvert.DeserializeObject>>(args.ToString()); BuildRoomPages(v.arguments[0]); } } // ==================== 分区 Tab ==================== private void BuildFenQuTabs() { if (tra_DaQuParent == null || tra_FenQuItem == null) return; var cfg = GameManager.Instance?.gameConfig?.roomConfigs; if (cfg == null) return; int gameType = (int)GameManager.Instance.gameData.currGameType; int leave = GetCurrentLeave(); int partitionCount = 1; foreach (var c in cfg) { if (c.gameType == gameType && c.roomLevel == leave) { partitionCount = c.roomPartitionNum > 0 ? c.roomPartitionNum : 1; break; } } for (int d = 1; d <= partitionCount; d++) { var tab = Instantiate(tra_FenQuItem, tra_DaQuParent); if (tab.childCount > 1 && tab.GetChild(1) != null) tab.GetChild(1).GetComponent().text = LevelNameFor(leave); if (tab.childCount > 2 && tab.GetChild(2) != null) tab.GetChild(2).GetComponent().text = "分区" + d; int district = d; var toggle = tab.GetComponent(); if (toggle != null) { toggle.onValueChanged.AddListener((isOn) => { if (!isOn) return; for (int i = 0; i < tra_DaQuParent.childCount; i++) { var t = tra_DaQuParent.GetChild(i).GetComponent(); if (t != null && t != toggle) t.SetIsOnWithoutNotify(false); } ClearRoomPages(); WebSocketSendList.GetRoomListFish(leave, district); }); } } } private int GetCurrentLeave() { var selectPanel = UIManager.Instance.Get(UIPanel.HomeMainPanel) ?.subUIManager.Get(UIPanel.SelectRoomLeavePenel); return selectPanel != null ? selectPanel.leave : 1; } private static string LevelNameFor(int leave) { return leave switch { 1 => "低级场", 2 => "中级场", 3 => "高级场", _ => "未知场" }; } // ==================== 房间列表 ==================== private void ClearRoomPages() { foreach (var item in itemPage) Destroy(item.gameObject); itemPage.Clear(); itemListDic.Clear(); text_PageNum.text = "0/0"; pageNum = 1; } private void BuildRoomPages(List rooms) { if (rooms == null || rooms.Count == 0) return; int pageMax = Mathf.CeilToInt(rooms.Count / 15f); for (int j = 0; j < pageMax; j++) itemPage.Add(Instantiate(itemPagePrefab, itemPageParent).transform); int i = 0; foreach (var item in rooms) { var tran = Instantiate(panel_RoomItemPrefab.gameObject, itemPage[i / 15]).transform; tran.GetComponent