Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I list rooms in Unity? (PUN2)

Tags:

c#

unity3d

photon

I need to list the romos there are in my lobby scene. For now, this is the code I've used but I don't know why it isnt working. Is this the correct way?

public override void OnRoomListUpdate(List<RoomInfo> roomList)
     {
         print(roomList.Count + " Rooms");
         base.OnRoomListUpdate(roomList);
         foreach (var Item in roomList)
         {
             RoomName = Item.Name;
             PlayerAmount = Item.PlayerCount;
             MaxPlayers = Item.MaxPlayers;
             PhotonNetwork.Instantiate("RoomPrefab", transform.position, transform.rotation);
             RoomPrefab.transform.Find("RoomName").GetComponent<Text>().text = RoomName;
             RoomPrefab.transform.Find("PlayerInt").GetComponent<Text>().text = PlayerAmount.ToString();
             if(MaxPlayers == 4)
             {
                 GameObject.Find("IPlayerA").GetComponent<Image>().sprite = Four;
             }
             else if (MaxPlayers == 2)
             {
                 GameObject.Find("IPlayerA").GetComponent<Image>().sprite = Two;
             }
             else if (MaxPlayers == 3)
             {
                 GameObject.Find("IPlayerA").GetComponent<Image>().sprite = Three;
             }
             RoomPrefab.transform.SetParent(ScrollView.transform, false);
         }
     }

I need to specify that I'm using Photon's PUN2, so GetRoomList won't work.

like image 586
Sebastián García Avatar asked Oct 17 '22 12:10

Sebastián García


2 Answers

PhotonNetwork.GetRoomList() is gone in PUN2. You get rooms list and updates from ILobbyCallbacks.OnRoomListUpdate(List roomList) callback. You can optionally cache it, update it and clear it when needed.

Also you can check updates from PUN to PUN2 here https://doc.photonengine.com/en-us/pun/v2/getting-started/migration-notes

like image 171
Saloni Bhoi Avatar answered Oct 20 '22 23:10

Saloni Bhoi


The OnRoomListUpdate() method is called only when you've explicitly joined the lobby via PhotonNetwork.JoinLobby(). It's not enough just to be connected to the MasterServer as Jevgeni Geurtsen suggested, at least in PUN v2.15 it works this way for me.

like image 29
Sergei Avatar answered Oct 21 '22 00:10

Sergei