• 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
1
Question by Michael_-01-_ · Apr 21, 2015 at 03:28 PM · multiplayerpun

Multiplayer lobby setup

Ok guys I need some help please, I have a game and its a multiplayer game, so its relativly set up but I need to make a multiplayer lobby, I have looked for tutorials on this and nothing is heplful enough, could someone please help me or point me to a video where I could learn this. Thanks guys

Check out the progress on my game! https://sites.google.com/site/whitegalaxystudiosltd/the-space-wars

Comment
Add comment · Show 1
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 Michael_-01-_ · Apr 22, 2015 at 06:46 PM 0
Share

I'm using photon unity networking if that helps

5 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by InfernoZYB · Apr 23, 2015 at 08:41 AM

I myself have worked with PUN and think its a great system as it is easy to use... but if you are new, you might run into a bit of problems because of its differences between unity networking. Anyway back to the answer. I will write a code below that you can use. To use it just make a new C# script and modify where is says: MYPREFAB near the bottom with you're character also make sure its in a resources folder. Make sure you make a gameobject and set it as SpawnSpot and change version to you're game version(Just so people with older game versions cant connect to people with newer. if you need more help comment below what you need and i will modify.

I also made a GIF of it in action: alt text

 using UnityEngine;
 using System.Collections.Generic;
 using System.Collections;
 
 public class NetworkManager : MonoBehaviour {
     
     private bool spawn = false;
     private int maxPlayer = 1;
     public GameObject SpawnSpot;
     private Room[] game;
     private string roomName = "DEFAULT ROOM NAME";
     bool connecting = false;
     List<string> chatMessages;
     int maxChatMessages = 5;
     private string maxPlayerString = "2";
     public string Version = "Version 1";
     private Vector3 up;
     private Vector2 scrollPosition;
     
     void Start (){
         PhotonNetwork.player.name = PlayerPrefs.GetString("Username", "My Player name");
         chatMessages = new List<string>();
     }
     
     void OnDestroy(){
         PlayerPrefs.SetString("Username", PhotonNetwork.player.name);
     }
     
     public void AddChatMessage(string m){
         GetComponent<PhotonView>().RPC("AddChatMessage_RPC", PhotonTargets.AllBuffered, m);
     }
     
     [RPC]
     void AddChatMessage_RPC(string m){
         while(chatMessages.Count >= maxChatMessages){
             chatMessages.RemoveAt(0);
         }
         chatMessages.Add(m);
     }
     
     void Connect(){
         PhotonNetwork.ConnectUsingSettings(Version);
     }
     
     void OnGUI(){
         GUI.color = Color.grey;
         if(PhotonNetwork.connected == false && connecting == false ) {
             GUILayout.BeginArea( new Rect(0, 0, Screen.width, Screen.height) );
             GUILayout.BeginHorizontal();
             GUILayout.FlexibleSpace();
             GUILayout.BeginVertical();
             GUILayout.FlexibleSpace();
             
             GUILayout.BeginHorizontal();
             GUILayout.Label("Username: ");
             PhotonNetwork.player.name = GUILayout.TextField(PhotonNetwork.player.name);
             GUILayout.EndHorizontal();
             
             if( GUILayout.Button("Multi Player") ) {
                 connecting = true;
                 Connect ();
             }
             GUILayout.FlexibleSpace();
             GUILayout.EndVertical();
             GUILayout.FlexibleSpace();
             GUILayout.EndHorizontal();
             GUILayout.EndArea();
         }
         if(PhotonNetwork.connected == true && connecting == false) {
             GUILayout.BeginArea( new Rect(0, 0, Screen.width, Screen.height) );
             GUILayout.BeginVertical();
             GUILayout.FlexibleSpace();
             
             foreach(string msg in chatMessages) {
                 GUILayout.Label(msg);
             }
             
             GUILayout.EndVertical();
             GUILayout.EndArea();
             
         }
         
         if (PhotonNetwork.insideLobby == true) {
             
             GUI.Box(new Rect(Screen.width/2.5f , Screen.height /3f , 400, 550),"");
             GUI.color = Color.white;
             GUILayout.BeginArea (new Rect(Screen.width/2.5f , Screen.height /3 , 400, 500));
             GUI.color = Color.cyan;
             GUILayout.Box ("Lobby");
             GUI.color = Color.white;
             
             GUILayout.Label("Session Name:");
             roomName = GUILayout.TextField(roomName);
             GUILayout.Label ("Max amount of players 1 - 20:");
             maxPlayerString = GUILayout.TextField (maxPlayerString,2);
             if (maxPlayerString != "") {
                 
                 maxPlayer = int.Parse (maxPlayerString);
                 
                 if (maxPlayer > 20) maxPlayer = 20;
                 if (maxPlayer == 0) maxPlayer = 1;
             }
             else
             {
                 maxPlayer = 1;
             }
             
             if ( GUILayout.Button ("Create Room ") ) {
                 if (roomName != "" && maxPlayer > 0) {
                     PhotonNetwork.CreateRoom(roomName,true,true,maxPlayer);
                 }
             }
             
             GUILayout.Space (20);
             GUI.color = Color.yellow;
             GUILayout.Box ("Sessions Open");
             GUI.color = Color.red;
             GUILayout.Space (20);
             
             scrollPosition = GUILayout.BeginScrollView(scrollPosition, false,true,GUILayout.Width(400), GUILayout.Height(300));
             
             
             foreach (RoomInfo game in PhotonNetwork.GetRoomList ())
             {
                 GUI.color = Color.green;
                 GUILayout.Box (game.name + " " + game.playerCount + "/" + game.maxPlayers + " " + game.visible);
                 if ( GUILayout.Button ("Join Session") ) {
                     PhotonNetwork.JoinRoom(game.name);
                 }
             }
             GUILayout.EndScrollView ();
             GUILayout.EndArea ();
         }
     }
     
     void OnJoinedLobby(){
         Debug.Log("OnJoinedLobby");
     }
     
     void OnPhotonRandomJoinFailed(){
         Debug.Log("OnPhotonRandomJoinFailed");
         PhotonNetwork.CreateRoom( null );
     }
     
     void OnJoinedRoom(){
         Debug.Log("OnJoinedRoom");
         connecting = false;
         spawn = true;
     }
     
     void Update(){
         if(spawn == true){
             spawn = false;
             GameObject MyCharacter = (GameObject)PhotonNetwork.Instantiate("MYPREFAB", SpawnSpot.transform.position, Quaternion.identity, 0);
         }
     }
 }


Comment
Add comment · Show 7 · 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 Michael_-01-_ · Apr 23, 2015 at 09:07 AM 0
Share

This is GREAT!! Thank you very much, but i do have a question about it. If for example a game was already going on, could one join the game? i ask this because the lobby will be set up at the menu, but the game wil go on in a other scene.

avatar image InfernoZYB · Apr 23, 2015 at 11:53 AM 0
Share

I'm not understanding fully what you want, like if you had a main menu scene then when they join it switches to a scene? If you want I could break this down into two scripts one for the main menu and one for when they join the room it switchs to the other scene and "spawns" them. Because the way it works now is you put this script on a component and it will work and when you join a room it spawns the "character" (If you need help. contact me on skype @: Contact_ZYB)

avatar image Michael_-01-_ · Apr 23, 2015 at 12:48 PM 0
Share

Yes, the actual lobby will be in the main menu, so I need to check for open lobbies, and then if they join it should load the scene, but there will be different classes so once in the game the spawn will happen from there. I am kinda familiar with the actual spawn of characters, I just need help with optaining open lobbies and joining them

avatar image InfernoZYB · Apr 23, 2015 at 09:13 PM 0
Share

Ok so here is what i have for you:

alt text

(Go ahead and delete or modify the script i have given to you and replace with newer ones) Ok so go to the Game Scene and put this script on any GameObject:

 using UnityEngine;
 using System.Collections.Generic;
 using System.Collections;
 
 public class Network$$anonymous$$anager : $$anonymous$$onoBehaviour {
 
     public GameObject SpawnSpot;
     private bool spawnandloaded;
 
     void OnLevelWasLoaded(int level){
         if (level == 1) { //Replace 1 with what your scene number is. You can check what it is in the build settings.
                 spawnandloaded = true;
         }
     }
     
     void Update(){
         if(spawnandloaded == true){
             spawnandloaded = false;
             GameObject $$anonymous$$yCharacter = (GameObject)PhotonNetwork.Instantiate("$$anonymous$$YPREFAB", SpawnSpot.transform.position, Quaternion.identity, 0);
         }
     }
 }

Next make another script and put it on any gameobject in the menu scene($$anonymous$$ake sure to set Gamescene to the name of your game scene!) [NOTICE: I have ran out of characters left and i cant post another answer so i will post a link to a pastebin with the code you put on any gameobject in the menu scene]

http://pastebin.com/a6jHk7Ra

(If this has helped you set it as the answer so more people will notice it! Also if you need more help just reply!)

avatar image hellriderz · Aug 04, 2015 at 12:43 PM 0
Share

i love the code but is there any way to change if from using the old GUI to the new one in unity 5?

thanks for sharing this beautiful code :)

Show more comments
avatar image
0

Answer by UDN_30011711-3d42-4608-a55a-5bf3a3109e16 · Nov 19, 2016 at 06:38 AM

@RavenOfCode I had this issue too. I had set isVisible=false;

Please set isVisible =true; in RoomOptions. It solved my problem.

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 aditya · Nov 19, 2016 at 06:44 AM 0
Share

Brother if setting isVisible to true solves your problem then how come my answer their is still not acepted ... not accepting an answer reducing the Correct percentage of an user and because of this reducing other user's belief on him too

avatar image
0

Answer by UDN_30011711-3d42-4608-a55a-5bf3a3109e16 · Nov 19, 2016 at 06:38 AM

@RavenOfCode I had this issue too. I had set isVisible=false;

Please set isVisible =true; in RoomOptions. It solved my problem.

Comment
Add comment · 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
0

Answer by SarthakHShah7 · May 29, 2017 at 06:32 AM

Do I have to connect to photon network once again if i load another multiplayer scene ? when i am loading scene from scene1 to scene2 , joinlobby is no getting executed. Can you please guide me on this ? I want to have total of 2 multiplyer scene and one starting scene where we create room and stuff. Is there any rule or constrains exist for using photon in difference scene ? I am new to photon.

Comment
Add comment · 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
0

Answer by SzKaz · Mar 30, 2018 at 09:20 PM

NetworkManager1.cs(110,20): error CS1502: The best overloaded method match for PhotonNetwork.CreateRoom(string, RoomOptions, TypedLobby, string[])' has some invalid arguments Assets/Resources 1/NetworkManager1.cs(110,40): error CS1503: Argument #2' cannot convert bool' expression to type RoomOptions'

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Callback for DNS entry error using Photon Networking 0 Answers

Spawn point for specific player.Photon Pun2 0 Answers

my multiplayer game second player not active 0 Answers

Photon Pun 2 "using photon.pun" not working. 3 Answers

Role Game Online (Using PUN) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges