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

220 lines
7.7 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;
2026-07-07 00:18:26 +08:00
public Transform tra_FenQuItem; // child1=场次名, child2=分区n
2026-07-06 22:18:28 +08:00
2026-02-03 11:35:19 +08:00
public Dictionary<string, Transform> itemListDic = new Dictionary<string, Transform>();
int pageNum = 1;
float horizontalNormalizedPosition;
2026-07-07 03:20:30 +08:00
private int _lastLeave = -1;
2026-07-06 22:18:28 +08:00
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);
2026-07-07 03:20:30 +08:00
int leave = GetCurrentLeave();
if (_lastLeave != leave)
2026-07-06 22:18:28 +08:00
{
2026-07-07 03:20:30 +08:00
// 场次变了 → 重建分区 Tab
for (int i = tra_DaQuParent.childCount - 1; i >= 0; i--)
Destroy(tra_DaQuParent.GetChild(i).gameObject);
2026-07-06 22:18:28 +08:00
BuildFenQuTabs();
2026-07-07 03:20:30 +08:00
_lastLeave = leave;
2026-07-06 22:18:28 +08:00
}
}
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);
if (tab.childCount > 1 && tab.GetChild(1) != null)
2026-07-06 23:11:31 +08:00
tab.GetChild(1).GetComponent<TextMeshProUGUI>().text = LevelNameFor(leave);
2026-07-06 22:18:28 +08:00
if (tab.childCount > 2 && tab.GetChild(2) != null)
tab.GetChild(2).GetComponent<TextMeshProUGUI>().text = "分区" + d;
int district = d;
2026-07-06 23:11:31 +08:00
var toggle = tab.GetComponent<Toggle>();
if (toggle != null)
2026-07-06 22:18:28 +08:00
{
2026-07-06 23:11:31 +08:00
toggle.onValueChanged.AddListener((isOn) =>
2026-07-06 22:18:28 +08:00
{
2026-07-06 23:11:31 +08:00
if (!isOn) return;
2026-07-07 00:18:26 +08:00
for (int i = 0; i < tra_DaQuParent.childCount; i++)
{
var t = tra_DaQuParent.GetChild(i).GetComponent<Toggle>();
if (t != null && t != toggle)
t.SetIsOnWithoutNotify(false);
}
2026-07-06 22:18:28 +08:00
ClearRoomPages();
WebSocketSendList.GetRoomListFish(leave, district);
});
}
}
}
private int GetCurrentLeave()
{
var selectPanel = UIManager.Instance.Get<UIHomeMain>(UIPanel.HomeMainPanel)
?.subUIManager.Get<UIHome_BY_SelectRoomLevelPanel>(UIPanel.SelectRoomLeavePenel);
return selectPanel != null ? selectPanel.leave : 1;
}
2026-07-06 23:11:31 +08:00
private static string LevelNameFor(int leave)
{
return leave switch { 1 => "低级场", 2 => "中级场", 3 => "高级场", _ => "未知场" };
}
2026-07-06 22:18:28 +08:00
// ==================== 房间列表 ====================
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;
2026-07-21 04:36:52 +08:00
UpdateRoomItemUI(tran, item.currentOnlinePlayerCount, item.robotCount, item.maxPlayerCount, item.roomNumber);
2026-07-06 22:18:28 +08:00
i++;
}
text_PageNum.text = "1/" + itemPage.Count;
scroll.horizontalNormalizedPosition = 0;
pageNum = 1;
2025-12-13 18:18:28 +08:00
}
2026-07-21 04:36:52 +08:00
/// <summary>
/// FishRoomHub RoomChange 通知:更新单个房间项的人数显示
/// </summary>
public void OnRoomChange(RoomChangeResponse room)
{
if (room == null || string.IsNullOrEmpty(room.roomId)) return;
if (!itemListDic.TryGetValue(room.roomId, out var tran) || tran == null) return;
UpdateRoomItemUI(tran, room.currentOnlinePlayerCount, room.robotCount, room.maxPlayerCount, room.roomNumber);
}
/// <summary>更新单个房间项的 UI(slider + 人数文本 + 房间号)</summary>
private void UpdateRoomItemUI(Transform tran, int currentOnline, int robot, int maxPlayer, int roomNumber)
{
var slider = tran.transform.GetChild(0).GetComponent<Slider>();
int allPlayerCount = currentOnline + robot;
if (slider != null)
{
slider.value = (float)allPlayerCount / (float)maxPlayer;
if (allPlayerCount >= maxPlayer && slider.fillRect != null && slider.fillRect.childCount > 0)
slider.fillRect.GetChild(0).gameObject.SetActive(true);
else if (allPlayerCount < maxPlayer && slider.fillRect != null && slider.fillRect.childCount > 0)
slider.fillRect.GetChild(0).gameObject.SetActive(false);
}
if (tran.transform.GetChild(0).childCount > 3 && tran.transform.GetChild(0).GetChild(3) != null)
tran.transform.GetChild(0).GetChild(3).GetComponent<TextMeshProUGUI>().text = allPlayerCount + "/" + maxPlayer;
if (tran.transform.childCount > 1 && tran.transform.GetChild(1) != null)
tran.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = roomNumber.ToString();
}
2025-12-13 18:18:28 +08:00
}