88 lines
3.4 KiB
C#
88 lines
3.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
//using System.Drawing;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
public class RoomManager : MonoBehaviour
|
|
{
|
|
public gameRoomList gameRoomListInstance; // 引用 gameRoomList 脚本实例
|
|
public GameRoomListResponse gameRoomListResponse;
|
|
private List<GameRoomListData> roomDataList;
|
|
private Transform contentParent; // Scroll View 的 Content 物体
|
|
public GameObject roomPrefab;//房间的预制体
|
|
public JSONReader js;
|
|
void Start()
|
|
{
|
|
gameRoomListInstance = FindObjectOfType<gameRoomList>();
|
|
contentParent =transform.Find("Mid/Scroll View/Viewport/Content").transform;
|
|
gameRoomListInstance = FindObjectOfType<gameRoomList>();
|
|
FetchRoomList();
|
|
}
|
|
// 异步获取房间数据
|
|
private async void FetchRoomList()
|
|
{
|
|
var response = await gameRoomListInstance.getGameRoomList();
|
|
|
|
if (response != null && response.Data != null)
|
|
{
|
|
roomDataList = response.Data;
|
|
Debug.Log($"获取到的房间数量:{roomDataList.Count}");
|
|
|
|
// 调用方法,动态生成房间元素
|
|
PopulateRoomList(roomDataList);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("获取房间列表失败!");
|
|
}
|
|
}
|
|
|
|
// 动态生成房间预制体,并赋值数据
|
|
private void PopulateRoomList(List<GameRoomListData> roomDataList)
|
|
{
|
|
foreach (var room in roomDataList)
|
|
{
|
|
// 实例化房间预制体
|
|
GameObject roomItem = Instantiate(roomPrefab, contentParent);
|
|
|
|
// 查找房间预制体中的 UI 组件并赋值
|
|
Text roomNameText = roomItem.transform.Find("down/detail").GetComponent<Text>();
|
|
Text roomStatusText = roomItem.transform.Find("up/type").GetComponent<Text>();
|
|
Button roomButton = roomItem.transform.GetComponent<Button>();
|
|
|
|
if (roomNameText != null) roomNameText.text = ""+js.SetUIText(room.SceneId)+" "+ js.SetUIText(room.SubjectId);
|
|
if (roomStatusText != null)
|
|
{
|
|
// 根据 room.Status 的值设置状态文本
|
|
switch (room.Status)
|
|
{
|
|
case "0":
|
|
roomStatusText.text = "已预订";
|
|
roomStatusText.color = new Color(255f / 255f, 255f / 255f, 0f / 255f);
|
|
break;
|
|
case "1":
|
|
roomStatusText.text = "进行中";
|
|
break;
|
|
case "2":
|
|
roomStatusText.text = "已结束";
|
|
roomStatusText.color = new Color(103f / 255f, 103f / 255f, 103f / 255f);
|
|
break;
|
|
default:
|
|
roomStatusText.text = "未知状态";
|
|
break;
|
|
}
|
|
}
|
|
// 添加 RoomDataHolder 脚本并存储数据
|
|
Status dataHolder = roomItem.GetComponent<Status>();
|
|
dataHolder.SetRoomData(room.RoomId, room.SceneId, room.SubjectId, room.TemplateId, room.Status);
|
|
// 为按钮添加点击事件,并传入房间数据
|
|
roomButton.onClick.AddListener(() => OnRoomButtonClicked(room));
|
|
}
|
|
}
|
|
// 房间按钮点击事件
|
|
private void OnRoomButtonClicked(GameRoomListData room)
|
|
{
|
|
Debug.Log($"点击了房间: {room.RoomId}, 状态: {room.Status}," +
|
|
$"模板ID: {room.TemplateId},场景ID: {room.SceneId},科目ID: {room.SubjectId}");
|
|
}
|
|
} |