- Home /
How to list the rooms that are there [PUN]
I am a extreme noob at this networking and GUI stuff and need some help.
I am trying to hook up my script to a button, that when you press it, it lists the all the available rooms that are there. I don't know where to go from what I have. I also have another button that joins the game that has a room selected, but I don't know how to do that either....
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class LobbyManger : MonoBehaviour { private CreateMatchManger Match;
private void Start()
{
Match = GetComponent<CreateMatchManger>();
}
public void ToMatchCreate()
{
SceneManager.LoadScene("Lobby Create Match");
}
public void ToMainMenu()
{
SceneManager.LoadScene("Main Menu");
}
public void OnGUI()
{
GetRoomsListed();
}
public void GetRoomsListed() { foreach(RoomInfo game in PhotonNetwork.GetRoomList()) { if (GUI.Button(new Rect(10, 70, 100, 50), Match.RoomName.ToString())){ JoinRoom(); } } }
private void JoinRoom()
{
PhotonNetwork.JoinRoom(Match.RoomName.ToString());
}
}
add delegate to join button. Use prefab and make room name,player count . join button ..when pressing join room delegate will help you to listed the rooms list
private void CreateRoomList() { //Debug.Log(PhotonNetwork.insideLobby); for (var i = RoomParent.transform.childCount - 1; i >= 0; i--) { // objectA is not the attached GameObject, so you can do all your checks with it. var obj = RoomParent.transform.GetChild(i); Destroy(obj.gameObject); // Optionally destroy the objectA if not longer needed }
//Debug.Log(PhotonNetwork.GetRoomList().Length.ToString());
foreach (var item in PhotonNetwork.GetRoomList())
{
GameObject go = Instantiate(RoomPrefabUI) as GameObject;
go.transform.SetParent(RoomParent.transform);
go.transform.localScale = new Vector3(1, 1, 1);
go.transform.GetChild(0).GetComponent<Text>().text = item.name;
go.transform.GetChild(1).GetComponent<Text>().text = item.playerCount + "/" + item.maxPlayers;
AttachOnClickEvent(go.transform.GetChild(2).gameObject.GetComponent<Button>(), item.name);
Debug.Log("Create Room prefab");
}
}
private void AttachOnClickEvent(Button button, string roomName)
{
button.onClick.AddListener(() => OnJoinRoomClicked(roomName));
}
public void OnJoinRoomClicked(string roomName)
{
PhotonNetwork.JoinRoom(roomName);
}
Answer by pradeepdmsp · Mar 12, 2017 at 03:19 PM
you can create delegate to join room button to selected room to join..
private void CreateRoomList() {
for (var i = RoomParent.transform.childCount - 1; i >= 0; i--)
{
// objectA is not the attached GameObject, so you can do all your checks with it.
var obj = RoomParent.transform.GetChild(i);
Destroy(obj.gameObject);
// Optionally destroy the objectA if not longer needed
}
foreach (var item in PhotonNetwork.GetRoomList())
{
GameObject go = Instantiate(RoomPrefabUI) as GameObject;
go.transform.SetParent(RoomParent.transform);
go.transform.localScale = new Vector3(1, 1, 1);
go.transform.GetChild(0).GetComponent<Text>().text = item.name;
go.transform.GetChild(1).GetComponent<Text>().text = item.playerCount + "/" +item.maxPlayers;
AttachOnClickEvent(go.transform.GetChild(2).gameObject.GetComponent<Button>(), item.name);
Debug.Log("Create Room prefab");
}
}
private void AttachOnClickEvent(Button button, string roomName)
{
button.onClick.AddListener(() => OnJoinRoomClicked(roomName));
}
public void OnJoinRoomClicked(string roomName) { PhotonNetwork.JoinRoom(roomName); }
Thank you for adding this code. I'm running into one issue. The Button to join isn't working because the code is constantly destroying and instantiating prefab ui.
Where do you place this CreateRoomList() method in order to make it call only when the GetRoomList() is changed?
Answer by ChristianSimon · Mar 08, 2017 at 09:30 AM
Hi,
when using OnGUI() function there is an easy way to achieve what you want. This also doesn't require any additional scripts e.g. the CreateMatchManager. You can try the following:
public void OnGUI()
{
if (PhotonNetwork.insideLobby)
{
if (GUILayout.Button("Create Game"))
{
PhotonNetwork.CreateRoom(PhotonNetwork.player.NickName);
}
foreach (RoomInfo room in PhotonNetwork.GetRoomList())
{
if (GUILayout.Button(room.Name.ToString()))
{
PhotonNetwork.JoinRoom(room.Name);
}
}
}
}
Please also make sure that you join the lobby after having successfully connected to Photon. You can do this by using OnConnectedToMaster() callback or - again the easy way - by navigating to the PhotonServerSettings file and check the Auto-Join Lobby option.
So, I tired plugging this in into my script and ran 2 stand alone windows, but when I created a match in 1 window, and then press the one you gave me, nothing really happened.
I guess I should have mention that there is another scene where I can create a match/room. The script above is for my main lobby area where you can join or go to the create match room area.
Your answer
Follow this Question
Related Questions
Correct way to design turn based multiplayer using Photon Unity Networking. 0 Answers
uNet like photon 0 Answers
Photon: I call PhotonNetwork.LeaveRoom() but my character doesn't leave 1 Answer
How to network a large map with 4000+ movable objects using Photon Unity Networking? 1 Answer
Callback for DNS entry error using Photon Networking 0 Answers