Legacy Documentation: Version 5.3
Converting a single-player game to Unity Multiplayer
Network Clients and Servers

Multiplayer Lobby

Many multiplayer games have a staging area (often known as a “lobby”) for players to join before playing the actual game. In this area, players can pick options and 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:

  • Limit on number of players that can join
  • Support for multiple players per client with a limit on number of players per client
  • Prevent players from joining game in-progress
  • Per-player ready state, so that game starts when all players are ready
  • Per-player configuration data
  • Re-joining the lobby when the game is finished
  • Virtual functions that allow custom logic for lobby events
  • A simple user interface for interacting with the lobby

The GuiLobbyManager is a special lobby manager that provides a user-interface for the lobby. It is available as an asset package and can be imported into Unity projects to easily add a lobby to multiplayer games. The scripts and UI prefabs in the package can be customized to make the look and feel of the lobby unique for any game.

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.

Lobby Player Objects

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:

  • One LobbyPlayer for each player
  • Created when client connects, or player is added
  • Exists until client disconnects
  • Holds the ready flag for this player for the lobby
  • Handles commands while in the lobby
  • Add user scripts to this prefab to hold game-specific player data
  • This prefab must have a NetworkLobbyPlayer component

Minimum Players

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:

  • Start one instance of the game and start Host. Then in game Lobby UI press “Start” for your player. You will still be in Lobby mode because minimum number of Ready players to start game is 2.
  • Start two more instances of the game and start Clients there. It doesn’t matter that “Minimum Players” set to 2. Wait for all - 3 in this case - connected players to become Ready.
  • Press “Start” in Lobby UI for one player. Two players are Ready, but still in Lobby mode. Press “Start” in the Lobby UI for the last player and now all players moved to Game mode.

GamePlayer

The GamePlayer is created from the GamePlayerPrefab when the game starts:

  • One GamePlayer for each player
  • Created when game scene is started
  • Destroyed when re-entering lobby
  • Handles commands while in the game
  • This prefab must have a NetworkIdentity component

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.

Adding the Lobby to a Game

Process for adding a NetworkLobby to a multiplayer game (without using the multiplayer-lobby asset package):

  • Create a new lobby scene
  • Add the scene to the build settings, as the first scene
  • Create a new game object in the new scene, rename it to LobbyManager
  • Add the NetworkLobbyManager component to the LobbyManager object
  • Add the NetworkManagerHUD component to the LobbyManager object
  • Open the inspector for the NetworkLobbyManager component
  • Set the LobbyScene slot of the NetworkLobbyManger to the scene that contains the LobbyManager object
  • Set the PlayScene slot of the NetworkLobbyManager to the main game-play scene for the game
  • Create a new gameObject and rename it to LobbyPlayer
  • Add the NetworkLobbyPlayer component to the LobbyPlayer
  • Create a prefab for the LobbyPlayer and delete the instance from the scene
  • Set the LobbyPlayerPrefab slot to the LobbyPlayer prefab
  • Set the GamePlayerPrefab slot to the prefab for the player in the main game
  • Save the scene.
  • Run the game

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

Sample Project

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.

Lobby Sample Project

Converting a single-player game to Unity Multiplayer
Network Clients and Servers