Most multiplayer games have a “lobby”; a SceneA Scene contains the environments and menus of your game. Think of each unique Scene file as a unique level. In each Scene, you place your environments, obstacles, and decorations, essentially designing and building your game in pieces. More info
See in Glossary in the game for players to join before playing the actual game. In the lobby, players can pick options and set themselves as ready for the game to start.
Unity provides the Network Lobby Manager component as a way for you to implement a Lobby for your game easily.
The Network Lobby Manager component provides a lobby for Unity Multiplayer games. It includes the following features:
Below are the Network Lobby Manager virtual methods. See API Reference documentation on the NetworkLobbyManager class for more details. There is a separate list for methods that are called on the client and on the server. You can write your own implementations for these methods to take action when any of these events occur.
NetworkLobbyManager
virtual methods called on the server:
NetworkLobbyManager
virtual methods called on the client:
All of the above server and client methods have empty default implementations, except for OnLobbyServerPlayersReady**
**, which calls ServerChangeScene with the PlayScene (the scene assigned to the Play Scene field in the Lobby Manager inspector.
There are two kinds of player Prefabs for the Lobby Manager: the Lobby Player PrefabAn asset type that allows you to store a GameObject complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. More info
See in Glossary and the** Game Player Prefab**. There is a field for each in the Network Lobby Manager component.
The Network Lobby Manager component
The Prefab that you assign to the Lobby Player Prefab slot must have a Network Lobby Player component attached. Each client that joins the Lobby gets a new Lobby player GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary, created from the Lobby Player Prefab. Unity creates the Lobby player GameObject when a client connects (that is, when a player joins the game), and it exists until the client disconnects.
The Network Lobby Player component holds the “ready” state for each player, and handles commands while in the lobby. You can add user scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary to the prefab to hold game-specific player data.
The Network Lobby Player component supplies some virtual method callbacks that can be used for custom lobby behaviour. These are:
public virtual void OnClientEnterLobby();
public virtual void OnClientExitLobby();
public virtual void OnClientReady(bool readyState);
Unity calls the method OnClientEnterLobby on the client when the game enters the lobby. This happens when the lobby Scene starts for the first time, and also when returning to the lobby from the gameplay Scene.
Unity calls the method OnClientExitLobby on the client when the game exits the lobby. This happens when switching to the gameplay Scene.
Unity calls the method OnClientReady on the client when the ready state of that player changes.
A game starts when all players have indicated they are ready. When the game starts, Unity creates a GameObject for each player, based on the the Game Player Prefab. Unity destroys these GameObjects at the end of the game, when players when re-enter the lobby. The Game Player Prefab handles commands while in the game. This prefab is a standard networked GameObject, and must have a Network Identity component attached.
On the Network Lobby Manager component, the Minimum Players field represents the minimum number of players that need to indicate that they are ready before the game starts. If the number of connected clients reaches the Minimum Players value, then waiting for all connected clients to become “Ready” starts the Match.
For example if “Minimum Players” is set to 2:
These steps outline the basic process for adding a Network Lobby to a multiplayer game using Unity’s built-in networkingThe Unity system that enables multiplayer gaming across a computer network. More info
See in Glossary features:
This version of the Network Lobby Manager is a very simple implementation, and uses a placeholder user interface, much like like the Network Manager HUD. Before you release your game, you should replace this with your own own user interface that matches your game’s visual design and feature requirements.
For an example of a better user interface, see the [multiplayer-lobby asset package]((https://www.assetstore.unity3d.com/en/#!/content/41836) available on the Asset Store.
The NetworkLobbyManager class has many virtual function callbacks for custom lobby behaviour. The most important function is OnLobbyServerSceneLoadedForPlayer, which is called on the server for each player when they transition from the lobby to the main game. This is the ideal place to apply settings from the lobby GameObject to the player GameObject.
// for users to apply settings from their lobby player GameObject to their in-game player GameObject
public override bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer)
{
var cc = lobbyPlayer.GetComponent<ColorControl>();
var player = gamePlayer.GetComponent<Player>();
player.myColor = cc.myColor;
return true;
}
There is a sample project on the** Unity Asset StoreA growing library of free and commercial assets created by Unity and members of the community. Offers a wide variety of assets, from textures, models and animations to whole Project examples, tutorials and Editor extensions. More info
See in Glossary** that uses the Network Lobby Manager and provides a GUI for the lobby. You can use this as a starting point for making your own Lobby for your multiplayer game. See Asset Store: Lobby Sample Project.