Version: 2019.3
Network Discovery
Using the Transport Layer API

Multiplayer Lobby

Note: UNet is deprecated, and will be removed from Unity in the future. A new system is under development. For more information and next steps see this blog post and the FAQ.

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:

  • A simple built-in user interface for interacting with the lobby
  • Limits the number of players that can join
  • Supports multiple players per client, with a limit on number of players per client
  • Prevents players from joining games that are in-progress
  • Supports a “ready” state for clients, so a game starts when all players are ready
  • Configuration data for each player
  • Players re-join the lobby when the game finishes
  • Virtual functions that allow custom logic for lobby events

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.

Lobby Player GameObjects

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

Lobby Player Prefab

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.

Game Player Prefab

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.

Minimum Players

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:

  • Start one instance of the game, and begin the game in host mode. Then in your game’s lobby interface, press “Start” for your player. You are still in the lobby, because the minimum number of ready players to start a game is 2.
  • Start two more instances of the game, and begin those games in client mode in those instances. Wait for all connected players (in this example, three) to become ready. Press “Start” in the Lobby UI(User Interface) Allows a user to interact with your application. More info
    See in Glossary
    for one player. Now, two players are ready, but still in the lobby. Press “Start” in the Lobby UI for the last player. All players move to the main game Scene.

Adding the Lobby to a game

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:

  • Create a new Scene for the lobby
  • Add the Scene to the Build Settings (File > Build Settings… > Add Open Scenes), as the first Scene
  • Create a new GameObject in the new Scene and rename it LobbyManager
  • Add the Network Lobby Manager component to the LobbyManager GameObject
  • Add the Network ManagerA Networking component that manages the network state of a Project. More info
    See in Glossary
    HUD component to the LobbyManager GameObject
  • Open the InspectorA Unity window that displays information about the currently selected GameObject, Asset or Project Settings, alowing you to inspect and edit the values. More info
    See in Glossary
    for the Network Lobby Manager component
  • In the Network Lobby Manager, set the Lobby Scene to the Scene that contains the LobbyManager GameObject
  • In the Network Lobby Manager, set the Play Scene to the main gameplay Scene for the game
  • Create a new GameObject and rename it LobbyPlayer
  • Add the Network Lobby Player component to the LobbyPlayer
  • Create a prefab for the LobbyPlayer GameObject, and delete the instance from the Scene
  • Set the LobbyPlayerPrefab field (in the Network Lobby Manager inspector) to the LobbyPlayer prefab
  • Set the GamePlayerPrefab field (in the Network Lobby Manager inspector) to the prefab for the player in the main game
  • Save the Scene
  • Run the game

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

Sample Project

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.

Network Discovery
Using the Transport Layer API
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961