请尊重原作者的工作,转载时请务必注明转载自:www.xionggf.com
 

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: NetworkLobbyManager类是一个特殊的NetworkManager。它为Unity多人游戏提供了一个游戏大厅。它包括有:
 
1 Limit on number of players that can join
2 Support for multiple players per client with a limit on number of players per client
3 Prevent players from joining game in-progress
4 Per-player ready state, so that game starts when all players are ready
5 Per-player configuration data
6 Re-joining the lobby when the game is finished
7 Virtual functions that allow custom logic for lobby events
8 A simple user interface for interacting with the lobby
1 能加入到游戏的人数限制。
2 通过限定每一个客户端上的player的人数,来支持每一个客户端上可以有多个玩家
3 阻止玩家在游戏处于进行状态时加入进来。
4 根据每一个玩家的准备状态,决定游戏是否开始
5 对每一个玩家进行数据配置。
6 当游戏结束时,让玩家重新进入到大厅
7 提供了若干虚函数,让用户重载定义,对大厅事件(lobby events)进行自定义的响应操作
8 提供一个简单的和大厅进行操作的用户接口
 
Below are the NetworkLobbyManager virtual functions called on the server: 下面是NetworkLobbyManager类的虚成员函数,它们在服务端被调用
 
OnLobbyStartHost
OnLobbyStopHost
OnLobbyStartServer
OnLobbyServerConnect
OnLobbyServerDisconnect
OnLobbyServerSceneChanged
OnLobbyServerCreateLobbyPlayer
OnLobbyServerCreateGamePlayer
OnLobbyServerPlayerRemoved
OnLobbyServerSceneLoadedForPlayer
OnLobbyServerPlayersReady
 
All of the above server functions have empty default implementations, except for OnLobbyServerPlayersReady, which calls ServerChangeScene with the PlayScene. 上述的所有服务端函数可以保持默认的实现。除了OnLobbyServerPlayersReady之外。该函数将会在PlayScene中调用ServerChangeScene方法
 
Below are the NetworkLobbyManager virtual functions called on the client: 下面的NetworkLobbyManager虚拟函数将会在客户端中被执行。
 
OnLobbyClientEnter
OnLobbyClientExit
OnLobbyClientConnect
OnLobbyClientDisconnect
OnLobbyStartClient
OnLobbyStopClient
OnLobbyClientSceneChanged
OnLobbyClientAddPlayerFailed
 
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: 有两种player object,其中一种是在NetworkLobbyManager注册有用来生成这player object的预设体的player object。注册到NetworkLobbyManager的槽位如下截图:
 
 
The LobbyPlayer is created from the LobbyPlayerPrefab when a player joins the lobby: 当一个player进入到大厅时,一个LobbyPlayer将会从LobbyPlayerPrefab预设中实例化出来。
 
1 One LobbyPlayer for each player
2 Created when client connects, or player is added
3 Exists until client disconnects
4 Holds the ready flag for this player for the lobby
5 Handles commands while in the lobby
6 Add user scripts to this prefab to hold game-specific player data
7 This prefab must have a NetworkLobbyPlayer component
1 每一个player对应一个LobbyPlayer
2 当客户端连上服务端,或者有玩家加入进来
3 当客户端和服务端断开连接时退出
4 维护大厅中的player的ready flag
5 当player在大厅中的时候,处理各种command
6 给prefab添加各种用户脚本,维护游戏特定的玩家数据
 
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.
“Minimun Players”
Minimum Players”属性表征了:大厅最少有多少个玩家处于就绪状态的player,才能进入游戏的数量值。如果进入大厅的客户端超过了“Minimum Players”所指定的数值,原本处于等待所有玩家都变成就绪状态的大厅,便可以开始游戏比赛(match)
 
For example if “Minimum Players” is set to 2: 例如,假设“Minimum Players”设置为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. 开启第一个游戏实例,以HOST模式进入。接着点击游戏大厅的UI界面的“Start”按钮。你将会仍然处于大厅中,因为“Minimum Players”的值为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. 然后启动两个游戏实例,以客户端模式进入。我们无需在意“Minimum Players”的值为2,这时候游戏会等待所有玩家变成就绪状态。
 
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. 然后再按下其中一个玩家的“start”按钮,这时候已经有两个player处于就绪状态了。但他们依然是在大厅的——因为就绪的player数还没大于“Minimum Players”的值。
 
GamePlayer
 
The GamePlayer is created from the GamePlayerPrefab when the game starts: 当游戏开始的时候,GamePlayer将会从GamePlayerPrefab预设中创建出来
 
1 One GamePlayer for each player
2 Created when game scene is started
3 Destroyed when re-entering lobby
4 Handles commands while in the game
5 This prefab must have a NetworkIdentity component
1 每一个玩家都有一个GamePlayer
2 当真正的游戏场景开始时GamePlayer对象被创建
3 当重新进入游戏大厅的时候GamePlayer对象被销毁
4 当在游戏中可以处理commands
5 创建GamePlayer的预设必须在根节点上挂接一个NetworkIdentity组件。
 
The NetworkLobbyPlayer component is used for LobbyPlayer objects. It supplies some virtual function callbacks that can be used for custom lobby behaviour NetworkLobbyPlayer组件用在LobbyPlayer对象上。它有一系列的虚回调函数,可供继承重载,实现自定义的大厅行为。
 
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. OnClientEnterLobby函数将在玩家进入大厅时,在客户端上被调用。注意只在大厅场景第一次启动时被执行。当然,当玩家从真正的游戏场景中返回到大厅时,也会被调用到。
 
The function OnClientExitLobby is called on the client when the game exists the lobby. This happens when switching to the game-play scene. OnClientExitLobby函数将在玩家离开大厅时,在客户端上被调用。这一般发生在玩家从大厅进入游戏场景时发生。
 
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):
要把NetworkLobby功能添加到一个多人游戏(不使用多人大厅资源包),可以按以下步骤执行。
 
1 Create a new lobby scene
2 Add the scene to the build settings, as the first scene
3 Create a new game object in the new scene, rename it to LobbyManager
4 Add the NetworkLobbyManager component to the LobbyManager object
5 Add the NetworkManagerHUD component to the LobbyManager object
6 Open the inspector for the NetworkLobbyManager component
7 Set the LobbyScene slot of the NetworkLobbyManger to the scene that contains the LobbyManager object
8 Set the PlayScene slot of the NetworkLobbyManager to the main game-play scene for the game
9 Create a new gameObject and rename it to LobbyPlayer
10 Add the NetworkLobbyPlayer component to the LobbyPlayer
11 Create a prefab for the LobbyPlayer and delete the instance from the scene
12 Set the LobbyPlayerPrefab slot to the LobbyPlayer prefab
13 Set the GamePlayerPrefab slot to the prefab for the player in the main game
14 Save the scene.
15 Run the game
1 创建一个新的大厅场景
2 在build settings窗口中添加此大厅场景,并且作为第一个场景
3 在该场景中创建一个game object,命名为LobbyManager
4 给LobbyManager game object上挂接一个NetworkLobbyManager组件。
5 给LobbyManager game object上挂接一个NetworkManagerHUD组件。
6 打开NetworkLobbyManager的inspector面板
7 把大厅场景拖放到面板中的LobbyScene槽位
8 把第一个游戏场景拖放到面板的PlayScene槽位
9 在大厅场景中,创建一个新的game object。重命名为LobbyPlayer
10 给LobbyPlayer game object挂接上NetworkLobbyPlayer组件。
11 创建LobbyPlayer的预设体
12 把LobbyPlayer的预设体拖放到LobbyManager面板上的LobbyPlayerPrefab槽位上。
13 把真正游戏中的用到的预设体,拖放到LobbyManager面板上的GamePlayerPrefab槽位上。
14 保存场景,运行游戏
 
This version of the NetworkLobbyManager uses the OnGUI user interface like the NetworkManagerHUD. For a better user interface use the multiplayer-lobby asset package.
和NetworkManagerHUD类似,当前版本的NetworkLobbyManager使用一个基于OnGUI的界面。在mulitplayer-lobby资源包里面有一个更好UI
 
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.
NetworkLobbyManager有很多虚拟回调成员函数。可供用户自定义大厅行为。最重要的一个回调函数是OnLobbyServerSceneLoadedForPlayer。当player从大厅切换到游戏场景中的时候,在服务端中该函数被调用的。这是一个设置player从大厅切换至游戏场景时的一些参数的合适场景
 
// 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;
}
 
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.
Unity在asset store上提供了一个使用了NetworkLobbyManager的大厅示例。该示例还提供了一个界面操作。可以通过它作为一个有大厅的多人游戏的起点