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. |
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 PrefabsAn 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 for the Lobby Manager: the Lobby Player Prefab 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.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.