Class NetworkLobbyManager
This is a specialized NetworkManager that includes a networked lobby.
The lobby has slots that track the joined players, and a maximum player count that is enforced. It requires that the NetworkLobbyPlayer component be on the lobby player objects.
NetworkLobbyManager is derived from NetworkManager, and so it implements many of the virtual functions provided by the NetworkManager class. To avoid accidentally replacing functionality of the NetworkLobbyManager, there are new virtual functions on the NetworkLobbyManager that begin with "OnLobby". These should be used on classes derived from NetworkLobbyManager instead of the virtual functions on NetworkManager.
The OnLobby*() functions have empty implementations on the NetworkLobbyManager base class, so the base class functions do not have to be called.
Inherited Members
Namespace: UnityEngine.Networking
Syntax
[AddComponentMenu("Network/NetworkLobbyManager")]
[Obsolete("The high level API classes are deprecated and will be removed in the future.")]
public class NetworkLobbyManager : NetworkManager
Fields
lobbySlots
These slots track players that enter the lobby.
The slotId on players is global to the game - across all players.
Declaration
public NetworkLobbyPlayer[] lobbySlots
Field Value
Type | Description |
---|---|
NetworkLobbyPlayer[] |
Properties
gamePlayerPrefab
This is the prefab of the player to be created in the PlayScene.
When CheckReadyToBegin starts the game from the lobby, a new player object is created from this prefab, and that object is made the active player object using NetworkServer.ReplacePlayerForConnection.
Declaration
public GameObject gamePlayerPrefab { get; set; }
Property Value
Type | Description |
---|---|
GameObject |
lobbyPlayerPrefab
This is the prefab of the player to be created in the LobbyScene.
This prefab must have a NetworkLobbyPlayer component on it.
In the lobby scene, this will be the active player object, but in other scenes while the game is running, this will be replaced by a player object created from the GamePlayerPrefab. But once returned to the lobby scene this will again become the active player object.
This can be used to store user data that persists for the lifetime of the session, such as color choices or weapon choices.
Declaration
public NetworkLobbyPlayer lobbyPlayerPrefab { get; set; }
Property Value
Type | Description |
---|---|
NetworkLobbyPlayer |
lobbyScene
The scene to use for the lobby. This is similar to the offlineScene of the NetworkManager.
Declaration
public string lobbyScene { get; set; }
Property Value
Type | Description |
---|---|
String |
maxPlayers
The maximum number of players allowed in the game.
Note that this is the number "players" not clients or connections. There can be multiple players per client.
Declaration
public int maxPlayers { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
maxPlayersPerConnection
The maximum number of players per connection.
Calling ClientScene.AddPlayer will fail if this limit is reached.
Declaration
public int maxPlayersPerConnection { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
minPlayers
The minimum number of players required to be ready for the game to start.
If this is zero then the game can start with any number of players.
Declaration
public int minPlayers { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
playScene
The scene to use for the playing the game from the lobby. This is similar to the onlineScene of the NetworkManager.
Declaration
public string playScene { get; set; }
Property Value
Type | Description |
---|---|
String |
showLobbyGUI
This flag enables display of the default lobby UI.
This is rendered using the old GUI system, so is only recommended for testing purposes.
Declaration
public bool showLobbyGUI { get; set; }
Property Value
Type | Description |
---|---|
Boolean |
Methods
CheckReadyToBegin()
CheckReadyToBegin checks all of the players in the lobby to see if their readyToBegin flag is set.
If all of the players are ready, then the server switches from the LobbyScene to the PlayScene - essentially starting the game. This is called automatically in response to NetworkLobbyPlayer.SendReadyToBeginMessage().
Declaration
public void CheckReadyToBegin()
OnClientConnect(NetworkConnection)
Declaration
public override void OnClientConnect(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn |
Overrides
OnClientDisconnect(NetworkConnection)
Declaration
public override void OnClientDisconnect(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn |
Overrides
OnClientSceneChanged(NetworkConnection)
Declaration
public override void OnClientSceneChanged(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn |
Overrides
OnLobbyClientAddPlayerFailed()
Called on the client when adding a player to the lobby fails.
This could be because the lobby is full, or the connection is not allowed to have more players.
Declaration
public virtual void OnLobbyClientAddPlayerFailed()
OnLobbyClientConnect(NetworkConnection)
This is called on the client when it connects to server.
Declaration
public virtual void OnLobbyClientConnect(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The connection that connected. |
OnLobbyClientDisconnect(NetworkConnection)
This is called on the client when disconnected from a server.
Declaration
public virtual void OnLobbyClientDisconnect(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The connection that disconnected. |
OnLobbyClientEnter()
This is a hook to allow custom behaviour when the game client enters the lobby.
Declaration
public virtual void OnLobbyClientEnter()
OnLobbyClientExit()
This is a hook to allow custom behaviour when the game client exits the lobby.
Declaration
public virtual void OnLobbyClientExit()
OnLobbyClientSceneChanged(NetworkConnection)
This is called on the client when the client is finished loading a new networked scene.
Declaration
public virtual void OnLobbyClientSceneChanged(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The connection that finished loading a new networked scene. |
OnLobbyServerConnect(NetworkConnection)
This is called on the server when a new client connects to the server.
Declaration
public virtual void OnLobbyServerConnect(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The new connection. |
OnLobbyServerCreateGamePlayer(NetworkConnection, Int16)
This allows customization of the creation of the GamePlayer object on the server.
By default the gamePlayerPrefab is used to create the game-player, but this function allows that behaviour to be customized. The object returned from the function will be used to replace the lobby-player on the connection.
Declaration
public virtual GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The connection the player object is for. |
Int16 | playerControllerId | The controllerId of the player on the connnection. |
Returns
Type | Description |
---|---|
GameObject | A new GamePlayer object. |
OnLobbyServerCreateLobbyPlayer(NetworkConnection, Int16)
This allows customization of the creation of the lobby-player object on the server.
By default the lobbyPlayerPrefab is used to create the lobby-player, but this function allows that behaviour to be customized.
Declaration
public virtual GameObject OnLobbyServerCreateLobbyPlayer(NetworkConnection conn, short playerControllerId)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The connection the player object is for. |
Int16 | playerControllerId | The controllerId of the player. |
Returns
Type | Description |
---|---|
GameObject | The new lobby-player object. |
OnLobbyServerDisconnect(NetworkConnection)
This is called on the server when a client disconnects.
Declaration
public virtual void OnLobbyServerDisconnect(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The connection that disconnected. |
OnLobbyServerPlayerRemoved(NetworkConnection, Int16)
This is called on the server when a player is removed.
Declaration
public virtual void OnLobbyServerPlayerRemoved(NetworkConnection conn, short playerControllerId)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | The connection the player object is for. |
Int16 | playerControllerId | The controllerId of the player that was removed. |
OnLobbyServerPlayersReady()
This is called on the server when all the players in the lobby are ready.
The default implementation of this function uses ServerChangeScene() to switch to the game player scene. By implementing this callback you can customize what happens when all the players in the lobby are ready, such as adding a countdown or a confirmation for a group leader.
using UnityEngine;
using UnityEngine.Networking;
public class GuiLobby : NetworkLobbyManager
{
float countTimer = 0;
public override void OnLobbyServerPlayersReady()
{
countTimer = Time.time + 5;
}
void Update()
{
if (countTimer == 0)
return;
if (Time.time > countTimer)
{
countTimer = 0;
ServerChangeScene(playScene);
}
else
{
Debug.Log("Counting down " + (countTimer - Time.time));
}
}
}
Declaration
public virtual void OnLobbyServerPlayersReady()
OnLobbyServerSceneChanged(String)
This is called on the server when a networked scene finishes loading.
Declaration
public virtual void OnLobbyServerSceneChanged(string sceneName)
Parameters
Type | Name | Description |
---|---|---|
String | sceneName | Name of the new scene. |
OnLobbyServerSceneLoadedForPlayer(GameObject, GameObject)
This is called on the server when it is told that a client has finished switching from the lobby scene to a game player scene.
When switching from the lobby, the lobby-player is replaced with a game-player object. This callback function gives an opportunity to apply state from the lobby-player to the game-player object.
Declaration
public virtual bool OnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer)
Parameters
Type | Name | Description |
---|---|---|
GameObject | lobbyPlayer | The lobby player object. |
GameObject | gamePlayer | The game player object. |
Returns
Type | Description |
---|---|
Boolean | False to not allow this player to replace the lobby player. |
OnLobbyStartClient(NetworkClient)
This is called on the client when a client is started.
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Example : NetworkLobbyManager
{
//Add this script to your GameObject. Make sure there isn't another NetworkManager in the Scene.
//Create 2 Buttons (Create>UI>Text) and either:
//1. assign them in the Inspector of the GameObject this script is attached to or
//2. remove this part and the listeners and alter the OnClick section on each Button to match up with each function
//Create a Text GameObject (Create>UI>Text) and attach it to the Status Text field in the Inspector.
public Button m_ClientButton, m_ServerButton;
bool m_ServerStarted, m_ClientStarted;
void Start()
{
showLobbyGUI = true;
//Call these functions when each Button is clicked
m_ServerButton.onClick.AddListener(ServerButton);
m_ClientButton.onClick.AddListener(ClientButton);
}
//Output a message when your client enters the lobby
public override void OnLobbyClientEnter()
{
m_ClientStarted = true;
base.OnLobbyClientEnter();
Debug.Log("Your client has entered the lobby!");
}
public override void OnLobbyStopClient()
{
Debug.Log("Client stopped");
base.OnLobbyStopClient();
}
public override void OnLobbyStartServer()
{
m_ServerStarted = true;
base.OnLobbyStartServer();
Debug.Log("Server Started!");
}
public override void OnStopServer()
{
m_ServerStarted = false;
base.OnStopServer();
Debug.Log("Server Stopped!");
}
//Start the Client when this Button is pressed
public void ClientButton()
{
if (m_ClientStarted == false)
{
StartClient();
m_ClientButton.GetComponentInChildren<Text>().text = "Stop Client";
}
else
{
StopClient();
m_ClientButton.GetComponentInChildren<Text>().text = "Start Client";
}
}
//Start the Server when this Button is pressed
public void ServerButton()
{
Debug.Log("Server : " + m_ServerStarted);
if (m_ServerStarted == false)
{
StartServer();
m_ServerButton.GetComponentInChildren<Text>().text = "Stop Server";
}
else
{
StopServer();
ServerReturnToLobby();
m_ServerButton.GetComponentInChildren<Text>().text = "Start Server";
}
}
}
Declaration
public virtual void OnLobbyStartClient(NetworkClient lobbyClient)
Parameters
Type | Name | Description |
---|---|---|
NetworkClient | lobbyClient | The connection for the lobby. |
OnLobbyStartHost()
This is called on the host when a host is started.
//This script shows you how to add extra functionality when the lobby host starts and stops
//Add this script to your GameObject. Make sure there isn't another NetworkManager in the Scene.
//Create a Host Button (Create>UI>Text) and assign it in the Inspector of the GameObject this script is attached to
//Create a Text GameObject (Create>UI>Text) and attach it to the Status Text field in the Inspector.
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Example : NetworkLobbyManager
{
public Button m_HostButton;
public Text m_StatusText;
bool m_HostStarted;
void Start()
{
//Set the minimum and maximum number of players
maxPlayers = 6;
minPlayers = 2;
maxPlayersPerConnection = 1;
//Call these functions when each Button is clicked
m_HostButton.onClick.AddListener(HostButton);
m_StatusText.text = "Current Scene : " + lobbyScene;
}
//Output a message when the host joins the lobby
public override void OnLobbyStartHost()
{
//Change the Text to show this message
m_StatusText.text = "A Host has joined the lobby!";
m_HostStarted = true;
//Do the default actions for when the host starts in the lobby
base.OnLobbyStartHost();
}
// Output a message to the host when the host stops
public override void OnLobbyStopHost()
{
//Output this message when the host stops
m_StatusText.text = "A Host has left the lobby!";
//Do the default actions when the host stops
base.OnLobbyStopHost();
m_HostStarted = false;
}
/// This is where the Buttons are given functionality
//Start the host when this Button is pressed
public void HostButton()
{
//Check if the host has already started
if (m_HostStarted == false)
{
//Start the host
StartHost();
//Change the Button's Text
m_HostButton.GetComponentInChildren<Text>().text = "Stop Host";
}
else
{
//If the host has already started, stop the host
StopHost();
//Change the Button's Text
m_HostButton.GetComponentInChildren<Text>().text = "Start Host";
}
}
}
Declaration
public virtual void OnLobbyStartHost()
OnLobbyStartServer()
This is called on the server when the server is started - including when a host is started.
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class Example : NetworkLobbyManager
{
//Add this script to your GameObject. Make sure there isn't another NetworkManager in the Scene.
//Create 2 Buttons (Create>UI>Text) and either:
//1. assign them in the Inspector of the GameObject this script is attached to or
//2. remove this part and the listeners and alter the OnClick section on each Button to match up with each function
//Create a Text GameObject (Create>UI>Text) and attach it to the Status Text field in the Inspector.
public Button m_ClientButton, m_ServerButton;
bool m_ServerStarted, m_ClientStarted;
void Start()
{
showLobbyGUI = true;
//Call these functions when each Button is clicked
m_ServerButton.onClick.AddListener(ServerButton);
m_ClientButton.onClick.AddListener(ClientButton);
}
//Output a message when your client enters the lobby
public override void OnLobbyClientEnter()
{
m_ClientStarted = true;
base.OnLobbyClientEnter();
Debug.Log("Your client has entered the lobby!");
}
public override void OnLobbyStopClient()
{
Debug.Log("Client stopped");
base.OnLobbyStopClient();
}
public override void OnLobbyStartServer()
{
m_ServerStarted = true;
base.OnLobbyStartServer();
Debug.Log("Server Started!");
}
public override void OnStopServer()
{
m_ServerStarted = false;
base.OnStopServer();
Debug.Log("Server Stopped!");
}
//Start the Client when this Button is pressed
public void ClientButton()
{
if (m_ClientStarted == false)
{
StartClient();
m_ClientButton.GetComponentInChildren<Text>().text = "Stop Client";
}
else
{
StopClient();
m_ClientButton.GetComponentInChildren<Text>().text = "Start Client";
}
}
//Start the Server when this Button is pressed
public void ServerButton()
{
Debug.Log("Server : " + m_ServerStarted);
if (m_ServerStarted == false)
{
StartServer();
m_ServerButton.GetComponentInChildren<Text>().text = "Stop Server";
}
else
{
StopServer();
ServerReturnToLobby();
m_ServerButton.GetComponentInChildren<Text>().text = "Start Server";
}
}
}
Declaration
public virtual void OnLobbyStartServer()
OnLobbyStopClient()
This is called on the client when the client stops.
Declaration
public virtual void OnLobbyStopClient()
OnLobbyStopHost()
This is called on the host when the host is stopped.
Declaration
public virtual void OnLobbyStopHost()
OnServerAddPlayer(NetworkConnection, Int16)
Declaration
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | |
Int16 | playerControllerId |
Overrides
OnServerConnect(NetworkConnection)
Declaration
public override void OnServerConnect(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn |
Overrides
OnServerDisconnect(NetworkConnection)
Declaration
public override void OnServerDisconnect(NetworkConnection conn)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn |
Overrides
OnServerRemovePlayer(NetworkConnection, PlayerController)
Declaration
public override void OnServerRemovePlayer(NetworkConnection conn, PlayerController player)
Parameters
Type | Name | Description |
---|---|---|
NetworkConnection | conn | |
PlayerController | player |
Overrides
OnServerSceneChanged(String)
Declaration
public override void OnServerSceneChanged(string sceneName)
Parameters
Type | Name | Description |
---|---|---|
String | sceneName |
Overrides
OnStartClient(NetworkClient)
Declaration
public override void OnStartClient(NetworkClient lobbyClient)
Parameters
Type | Name | Description |
---|---|---|
NetworkClient | lobbyClient |
Overrides
OnStartHost()
Declaration
public override void OnStartHost()
Overrides
OnStartServer()
Declaration
public override void OnStartServer()
Overrides
OnStopClient()
Declaration
public override void OnStopClient()
Overrides
OnStopHost()
Declaration
public override void OnStopHost()
Overrides
SendReturnToLobby()
Sends a message to the server to make the game return to the lobby scene.
Declaration
public bool SendReturnToLobby()
Returns
Type | Description |
---|---|
Boolean | True if message was sent. |
ServerChangeScene(String)
Declaration
public override void ServerChangeScene(string sceneName)
Parameters
Type | Name | Description |
---|---|---|
String | sceneName |
Overrides
ServerReturnToLobby()
Calling this causes the server to switch back to the lobby scene.
Declaration
public void ServerReturnToLobby()
TryToAddPlayer()
This is used on clients to attempt to add a player to the game.
This may fail if the game is full or the connection cannot have more players.
Declaration
public void TryToAddPlayer()