57 lines
1.5 KiB
C#
57 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json;
|
|
using Photon.Pun;
|
|
using Photon.Realtime;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class TestRoomExplorer : MonoBehaviourPunCallbacks
|
|
{
|
|
public Transform roomListParent;
|
|
public GameObject roomListItemPrefab;
|
|
public GameObject RoomInfoPanel;
|
|
public TMP_Text infoText;
|
|
|
|
|
|
|
|
void Start()
|
|
{
|
|
PopulateRoomList();
|
|
PhotonNetwork.ConnectUsingSettings();
|
|
}
|
|
|
|
public override void OnConnectedToMaster()
|
|
{
|
|
base.OnConnectedToMaster();
|
|
|
|
PhotonNetwork.JoinLobby();
|
|
}
|
|
public static List<RoomInfo> rooms = new List<RoomInfo>();
|
|
public override void OnRoomListUpdate(List<RoomInfo> roomList)
|
|
{
|
|
base.OnRoomListUpdate(roomList);
|
|
rooms = roomList;
|
|
PopulateRoomList();
|
|
}
|
|
|
|
void PopulateRoomList(){
|
|
roomListParent.PurgeChildren();
|
|
|
|
foreach(RoomInfo room in rooms){
|
|
GameObject newListItem = Instantiate(roomListItemPrefab, roomListParent);
|
|
newListItem.transform.GetChild(0).GetComponent<TMP_Text>().text = room.Name;
|
|
newListItem.transform.GetChild(1).GetComponent<TMP_Text>().text = room.PlayerCount.ToString();
|
|
|
|
newListItem.GetComponent<Button>().onClick.AddListener(()=>{ShowRoomInfo(room);});
|
|
}
|
|
}
|
|
|
|
public void ShowRoomInfo(RoomInfo info){
|
|
RoomInfoPanel.SetActive(true);
|
|
|
|
infoText.text = JsonConvert.SerializeObject(info);
|
|
}
|
|
}
|