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. Its functionality includes the following:
Abajo hay unas funciones virtuales NetworkLobbyManager llamadas en el servidor:
All of the above server functions have empty default implementations, except for OnLobbyServerPlayersReady
, which calls ServerChangeScene
with the PlayScene.
Abajo están las funciones virtuales NetworkLobbyManager llamada en el cliente:
Todas las funciones cliente de arriba tienen una implementación vacía predeterminada.
There are two kinds of player objects, each of which has a Prefab slot in the NetworkLobbyManager. The slots can be seen in this screenshot:
El LobbyPlayer es creado del LobbyPlayerPrefab cuando un jugador se une al 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” starts the Match.
Por ejemplo si “Minimum Players” es configurado a 2:
El GamePlayer es creado del GamePlayerPrefab cuando el juego empieza:
El componente NetworkLobbyPlayer es utilizado para objetos LobbyPlayer. Este proporciona algunos callbacks de funciones virtuales que pueden ser utilizados para un comportamiento de lobby personalizado
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 gameplay Scene.
The function OnClientExitLobby is called on the client when the game exits the lobby. This happens when switching to the gameplay Scene.
La función OnClientReady se llama en el cliente cuando el estado ready del jugador cambia.
Proceso de agregar un NetworkLobby a un juego multi-jugador (sin utilizar el paquete de asset multiplayer-lobby):
This version of the NetworkLobbyManager uses the OnGUI user interface like the NetworkManagerHUD. For a better user interface, use the multiplayer-lobby Asset package (found in the NetworkStarter sample package, on the Unity forums).
The NetworkLobbyManager has many virtual function callbacks that can be used for custom lobby behaviour. The 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 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;
}
Hay un proyecto ejemplo en el Unity asset store que utiliza el NetworkLobbyManager y proporciona un GUI para el lobby. Este puede ser utilizada como punto inicial para crear un jugador multi-jugador con un lobby.