Many multiplayer games have a staging area for players to join before playing the actual game. In this area - often called the “lobby”, players may be able to pick options and be able to set themselves as ready for the game to start.
The NetworkLobbyManager is a specialized NetworkManager that provides a lobby for Unity Multiplayer games. It includes:
Below are the NetworkLobbyManager virtual functions called on the server:
All of the above server functions have empty default implementations, except for OnLobbyServerPlayersReady, which calls ServerChangeScene with the PlayScene.
Below are the NetworkLobbyManager virtual functions called on the client:
All of the above client functions have empty default implementations.
There are two kinds of player objects - each which has a prefab slot in the NetworkLobbyManager. The slots can be seen in this screenshot:
The LobbyPlayer is created from the LobbyPlayerPrefab when a player joins the lobby:
The “Minimum Players” field represents the minimum number of “Ready” players in the Lobby to start the Match with. If the number of connected clients is more than the “Minimum Players” value, then waiting for all connected clients to become “Ready” will start the Match.
For example if “Minimum Players” is set to 2:
The GamePlayer is created from the GamePlayerPrefab when the game starts:
The NetworkLobbyPlayer component is used for LobbyPlayer objects. It supplies some virtual function callbacks that can be used for custom lobby behaviour
public virtual void OnClientEnterLobby();
public virtual void OnClientExitLobby();
public virtual void OnClientReady(bool readyState);
The function OnClientEnterLobby is called 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 game-play scene.
The function OnClientExitLobby is called on the client when the game exists the lobby. This happens when switching to the game-play scene.
The function OnClientReady is called on the client when the ready state of that player changes.
Process for adding a NetworkLobby to a multiplayer game (without using the multiplayer-lobby asset package):
This version of the NetworkLobbyManager uses the OnGUI user interface like the NetworkManagerHUD. For a better user interface use the multiplayer-lobby asset package.
The NetworkLobbyManager has many virtual function callbacks that can be used for custom lobby behaviour. Most important is OnLobbyServerSceneLoadedForPlayer which is called on the server for each player as they transition from the lobby to the main game. This is the ideal place to apply settings from the lobby to the player’s game-play object.
// for users to apply settings from their lobby player object to their in-game player object
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 store that uses the NetworkLobbyManager and provides a GUI for the lobby. This can be used as a starting point for making a multiplayer game with a lobby.