Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NovaDrox · Mar 08, 2017 at 05:14 AM · networkingphotonlistspun

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());
 }

}

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pradeepdmsp · Mar 08, 2017 at 03:34 PM 0
Share

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

avatar image NovaDrox pradeepdmsp · Mar 08, 2017 at 03:54 PM 0
Share

Could you possibly show me that?

Here's my project: link text

my-game.zip (66.5 kB)
avatar image pradeepdmsp NovaDrox · Mar 08, 2017 at 03:59 PM 0
Share

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);

 }
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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); }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ddelorme · May 10, 2017 at 06:31 PM 0
Share

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?

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NovaDrox · Mar 08, 2017 at 01:15 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges