_xiaofang/xiaofang/Assets/Res/gsj/scripts/RoomManager.cs

112 lines
4.2 KiB
C#
Raw Normal View History

2024-12-25 22:10:20 +08:00
using System;
2024-12-20 10:10:19 +08:00
using System.Collections;
using System.Collections.Generic;
//using System.Drawing;
using UnityEngine;
2024-12-25 22:10:20 +08:00
using UnityEngine.SceneManagement;
2024-12-20 10:10:19 +08:00
using UnityEngine.UI;
public class RoomManager : MonoBehaviour
{
public gameRoomList gameRoomListInstance; // <20><><EFBFBD><EFBFBD> gameRoomList <20>ű<EFBFBD>ʵ<EFBFBD><CAB5>
public GameRoomListResponse gameRoomListResponse;
private List<GameRoomListData> roomDataList;
private Transform contentParent; // Scroll View <20><> Content <20><><EFBFBD><EFBFBD>
public GameObject roomPrefab;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>
public JSONReader js;
void Start()
{
gameRoomListInstance = FindObjectOfType<gameRoomList>();
contentParent =transform.Find("Mid/Scroll View/Viewport/Content").transform;
gameRoomListInstance = FindObjectOfType<gameRoomList>();
FetchRoomList();
}
2024-12-25 22:10:20 +08:00
2024-12-20 10:10:19 +08:00
// <20><EFBFBD><ECB2BD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private async void FetchRoomList()
{
var response = await gameRoomListInstance.getGameRoomList();
if (response != null && response.Data != null)
{
roomDataList = response.Data;
Debug.Log($"<22><>ȡ<EFBFBD><C8A1><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>{roomDataList.Count}");
// <20><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD><CCAC><EFBFBD>ɷ<EFBFBD><C9B7><EFBFBD>Ԫ<EFBFBD><D4AA>
PopulateRoomList(roomDataList);
}
else
{
Debug.LogError("<22><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD>");
}
}
// <20><>̬<EFBFBD><CCAC><EFBFBD>ɷ<EFBFBD><C9B7><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><E5A3AC><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>
private void PopulateRoomList(List<GameRoomListData> roomDataList)
{
2024-12-25 22:10:20 +08:00
2024-12-20 10:10:19 +08:00
foreach (var room in roomDataList)
{
// ʵ<><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>
GameObject roomItem = Instantiate(roomPrefab, contentParent);
// <20><><EFBFBD>ҷ<EFBFBD><D2B7><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> UI <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
Text roomNameText = roomItem.transform.Find("down/detail").GetComponent<Text>();
Text roomStatusText = roomItem.transform.Find("up/type").GetComponent<Text>();
Button roomButton = roomItem.transform.GetComponent<Button>();
2024-12-24 23:02:32 +08:00
if (roomNameText != null) roomNameText.text = ""+js.SetUIText(room.SceneId)+" "+ js.SetUIText(room.SubjectId);
2024-12-20 10:10:19 +08:00
if (roomStatusText != null)
{
// <20><><EFBFBD><EFBFBD> room.Status <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>״̬<D7B4>ı<EFBFBD>
switch (room.Status)
{
case "0":
roomStatusText.text = "<22><>Ԥ<EFBFBD><D4A4>";
roomStatusText.color = new Color(255f / 255f, 255f / 255f, 0f / 255f);
break;
case "1":
roomStatusText.text = "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
break;
case "2":
roomStatusText.text = "<22>ѽ<EFBFBD><D1BD><EFBFBD>";
roomStatusText.color = new Color(103f / 255f, 103f / 255f, 103f / 255f);
break;
default:
roomStatusText.text = "δ֪״̬";
break;
}
}
// <20><><EFBFBD><EFBFBD> RoomDataHolder <20>ű<EFBFBD><C5B1><EFBFBD><EFBFBD><EFBFBD><E6B4A2><EFBFBD><EFBFBD>
Status dataHolder = roomItem.GetComponent<Status>();
dataHolder.SetRoomData(room.RoomId, room.SceneId, room.SubjectId, room.TemplateId, room.Status);
2024-12-25 22:10:20 +08:00
// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
DateTime currentDate = DateTime.Now;
//<2F><><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD>ڸ<EFBFBD>ʽ<EFBFBD><CABD>Ϊ "yyyy/MM/dd" <20><>ʽ
string formattedDate = currentDate.ToString("yyyy/MM/dd");
for (int i = 0; i < room.PlayerList.Count; i++)
{
if (GlobalData.ServerData.data.userId== room.PlayerList[i].UserId)
{
Debug.Log("<22><><EFBFBD><EFBFBD>ֱ<EFBFBD>ӽ<EFBFBD><D3BD>뷿<EFBFBD><EBB7BF>");
//SceneManager.LoadScene(2);
}
Debug.Log("UserId=" + room.PlayerList[i].UserId);
}
2024-12-20 10:10:19 +08:00
// Ϊ<><CEAA>ť<EFBFBD><C5A5><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뷿<EFBFBD><EBB7BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
roomButton.onClick.AddListener(() => OnRoomButtonClicked(room));
}
}
// <20><><EFBFBD>䰴ť<E4B0B4><C5A5><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD>
private void OnRoomButtonClicked(GameRoomListData room)
{
Debug.Log($"<22><><EFBFBD><EFBFBD><EFBFBD>˷<EFBFBD><CBB7><EFBFBD>: {room.RoomId}, ״̬: {room.Status}," +
$"ģ<><C4A3>ID: {room.TemplateId},<2C><><EFBFBD><EFBFBD>ID: {room.SceneId},<2C><>ĿID: {room.SubjectId}");
2024-12-25 22:10:20 +08:00
Debug.Log("ReserveDate=" + room.ReserveDate);
for(int i=0;i<room.PlayerList.Count;i++)
{
Debug.Log("UserId=" + room.PlayerList[i].UserId);
}
2024-12-20 10:10:19 +08:00
}
}