Files
5.24BuYu/Assets/MyGame/aa/UI/Home/Sub/UIHome_BY_MySelectRoomPanel.cs
T

207 lines
7.0 KiB
C#
Raw Normal View History

2026-07-06 22:18:28 +08:00
using Assets.MyGame.Scripts.Net;
using Assets.MyGame.Scripts.Net.API.GameConfig_SubClass;
2026-02-03 11:35:19 +08:00
using Assets.MyGame.Scripts.Net.WebResponses;
using Newtonsoft.Json;
2025-12-13 18:18:28 +08:00
using System.Collections.Generic;
2026-02-03 11:35:19 +08:00
using TMPro;
2025-12-13 18:18:28 +08:00
using UnityEngine;
using UnityEngine.UI;
2025-12-26 11:15:56 +08:00
public class UIHome_BY_MySelectRoomPanel : SubUIBase
2025-12-13 18:18:28 +08:00
{
public GameObject panel_PanelParent;
public Button panel_RoomItemPrefab;
2026-02-03 11:35:19 +08:00
public Button btn_NextPage;
public Button btn_UpPage;
public TextMeshProUGUI text_PageNum;
public ScrollRect scroll;
public List<Transform> itemPage = new List<Transform>();
public GameObject itemPagePrefab;
public Transform itemPageParent;
2026-07-06 22:18:28 +08:00
public Transform tra_DaQuParent;
public Transform tra_FenQuItem; // child0=背景, child1=场次名, child2=分区n
2026-02-03 11:35:19 +08:00
public Dictionary<string, Transform> itemListDic = new Dictionary<string, Transform>();
int pageNum = 1;
float horizontalNormalizedPosition;
2026-07-06 22:18:28 +08:00
private bool _tabsBuilt = false;
2025-12-13 18:18:28 +08:00
public override void Awake()
{
2026-02-03 11:35:19 +08:00
btn_NextPage.onClick.AddListener(() =>
{
2026-07-06 22:18:28 +08:00
if (pageNum > 1) pageNum--;
2026-02-03 11:35:19 +08:00
text_PageNum.text = pageNum + "/" + itemPage.Count;
horizontalNormalizedPosition = (float)(pageNum - 1) / (float)(itemPage.Count - 1);
});
btn_UpPage.onClick.AddListener(() =>
{
2026-07-06 22:18:28 +08:00
if (pageNum < itemPage.Count) pageNum++;
2026-02-03 11:35:19 +08:00
text_PageNum.text = pageNum + "/" + itemPage.Count;
horizontalNormalizedPosition = (float)(pageNum - 1) / (float)(itemPage.Count - 1);
});
2026-07-06 22:18:28 +08:00
btn_Close.onClick.AddListener(() => Hide());
2025-12-13 18:18:28 +08:00
}
2026-07-06 22:18:28 +08:00
2026-02-03 11:35:19 +08:00
private void Update()
{
scroll.horizontalNormalizedPosition = Mathf.Lerp(scroll.horizontalNormalizedPosition, horizontalNormalizedPosition, 0.5f);
}
2026-07-06 22:18:28 +08:00
2025-12-13 18:18:28 +08:00
public override void Hide()
{
base.Hide();
panel_PanelParent.SetActive(true);
}
2026-07-06 22:18:28 +08:00
protected override void OnShow(object args = null)
{
base.OnShow(args);
if (!_tabsBuilt)
{
BuildFenQuTabs();
_tabsBuilt = true;
}
}
2025-12-13 18:18:28 +08:00
public override void Show(object args = null)
{
base.Show(args);
2026-07-06 22:18:28 +08:00
ClearRoomPages();
2026-02-03 11:35:19 +08:00
if (args != null)
{
var v = JsonConvert.DeserializeObject<WebSocketResponseBase<List<GetRoomList>>>(args.ToString());
2026-07-06 22:18:28 +08:00
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)
2026-02-03 11:35:19 +08:00
{
2026-07-06 22:18:28 +08:00
partitionCount = c.roomPartitionNum > 0 ? c.roomPartitionNum : 1;
break;
2026-02-03 11:35:19 +08:00
}
}
2026-07-06 22:18:28 +08:00
for (int d = 1; d <= partitionCount; d++)
{
var tab = Instantiate(tra_FenQuItem, tra_DaQuParent);
// child1 = 场次名(隐藏,等级已在外层选好)
if (tab.childCount > 1 && tab.GetChild(1) != null)
tab.GetChild(1).gameObject.SetActive(false);
// child2 = 分区n
if (tab.childCount > 2 && tab.GetChild(2) != null)
tab.GetChild(2).GetComponent<TextMeshProUGUI>().text = "分区" + d;
int district = d;
var btn = tab.GetComponent<Button>();
if (btn != null)
{
CanvasRenderer cr = tab.GetComponent<CanvasRenderer>();
if (cr == null) cr = tab.GetChild(0).GetComponent<CanvasRenderer>();
btn.onClick.AddListener(() =>
{
// 高亮选中
for (int i = 0; i < tra_DaQuParent.childCount; i++)
{
var t = tra_DaQuParent.GetChild(i);
var b = t.GetComponent<Button>();
if (b != null)
{
var colors = b.colors;
colors.normalColor = t == tab ? new Color(1f, 0.85f, 0.3f) : Color.white;
b.colors = colors;
}
}
ClearRoomPages();
WebSocketSendList.GetRoomListFish(leave, district);
});
}
// 默认加载分区1
if (d == 1)
WebSocketSendList.GetRoomListFish(leave, 1);
}
}
private int GetCurrentLeave()
{
var selectPanel = UIManager.Instance.Get<UIHomeMain>(UIPanel.HomeMainPanel)
?.subUIManager.Get<UIHome_BY_SelectRoomLevelPanel>(UIPanel.SelectRoomLeavePenel);
return selectPanel != null ? selectPanel.leave : 1;
}
// ==================== 房间列表 ====================
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<GetRoomList> 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<Button>().onClick.AddListener(() =>
{
WebSocketSendList.GetRoomInfo(item.roomId);
});
tran.name = item.roomId;
itemListDic[item.roomId] = tran;
var slider = tran.transform.GetChild(1).GetComponent<Slider>();
int allPlayerCount = item.currentOnlinePlayerCount + item.robotCount;
if (slider != null)
{
slider.value = (float)allPlayerCount / (float)item.maxPlayerCount;
if (allPlayerCount >= item.maxPlayerCount && slider.fillRect != null && slider.fillRect.childCount > 0)
slider.fillRect.GetChild(0).gameObject.SetActive(true);
}
if (tran.transform.childCount > 2 && tran.transform.GetChild(2) != null)
tran.transform.GetChild(2).GetComponent<TextMeshProUGUI>().text = allPlayerCount + "/" + item.maxPlayerCount;
if (tran.transform.childCount > 3 && tran.transform.GetChild(3) != null)
tran.transform.GetChild(3).GetComponent<TextMeshProUGUI>().text = item.roomNumber.ToString();
i++;
}
text_PageNum.text = "1/" + itemPage.Count;
scroll.horizontalNormalizedPosition = 0;
pageNum = 1;
2025-12-13 18:18:28 +08:00
}
}