Multiplayer Lobby

多人游戏大厅

Many multiplayergames have a staging area for players to join before playing the actual game.In this area - often called the “lobby”, players may be able to pick optionsand be able to set themselves as ready for the game to start.

大部分多人在线游戏都会有一个让玩家在加入正式游戏前的临时中转区。在这个区域里通常我们管它叫做大厅,玩家可以进行选项的设置,进行游戏开始前的各种准备。

TheNetworkLobbyManager is a specialized NetworkManager thatprovides a lobby for Unity Multiplayer games. It includes:

NetworkLobbyManager(联网大厅管理器)是一个专门为unity在线游戏提供一个大厅的NetworkManager(联网管理器)。它包含以下内容:

Limit on numberof players that can join

Support formultiple players per client with a limit on number of players per client

Prevent playersfrom joining game in-progress

Per-player readystate, so that game starts when all players are ready

Per-playerconfiguration data

Re-joining thelobby when the game is finished

Virtual functionsthat allow custom logic for lobby events

A simple userinterface for interacting with the lobby

为每个客户端上的每个玩家提供有限的其他其他玩家

防止玩家在游戏进行中的时候加入

每个玩家的准备状态,游戏将会在所有玩家都准备好的情况下开始

每个玩家的设置数据

当游戏完成的时候重新加入大厅

提供大厅事件的虚函数供开发人员进行自定义逻辑的开发

提供简单的用户界面给大厅做交互

TheGuiLobbyManager is a special lobby manager that provides a user-interface forthe lobby. It is available as an asset package and can be imported into Unityprojects to easily add a lobby to multiplayer games. The scripts and UI prefabsin the package can be customized to make the look and feel of the lobby uniquefor any game.

GuiLobbyManager(大厅界面管理器)是一个为大厅提供用户界面的特殊的大厅管理器。它可以被打包成一个资源包,便于导入到一个Unity多人在线项目来添加一个大厅。

Below are the NetworkLobbyManagervirtual functions called on the server:

以下是NetworkLobbyManager(网络大厅管理器)在服务器上被调用的虚函数:

OnLobbyStartHost// 开启一个大厅主机时

OnLobbyStopHost// 停止一个大厅主机时

OnLobbyStartServer// 开启一个大厅服务器时

OnLobbyServerConnect// 连接一个大厅服务器时

OnLobbyServerDisconnect// 大厅服务器关闭时

OnLobbyServerSceneChanged// 大厅服务器切换场景时

OnLobbyServerCreateLobbyPlayer// 大厅服务器创建了一个大厅玩家

OnLobbyServerCreateGamePlayer// 大厅服务器创建了一个游戏玩家

OnLobbyServerPlayerRemoved// 大厅服务器玩家被移除

OnLobbyServerSceneLoadedForPlayer// 玩家加载大厅服务器场景时

OnLobbyServerPlayersReady// 大厅玩家准备时

All of the aboveserver functions have empty default implementations, except forOnLobbyServerPlayersReady, which calls ServerChangeScene with the PlayScene.

以上所有服务器方法除了OnLobbyServerPlayersReady(为PlayScene调用ServerChangeScene)都有默认空的实现,

Below are theNetworkLobbyManager virtual functions called on the client:

以下NetworkLobbyManager(联网大厅管理器)虚函数在客户端调用

OnLobbyClientEnter// 大厅客户端进入

OnLobbyClientExit// 大厅客户端退出

OnLobbyClientConnect// 大厅客户端连接

OnLobbyClientDisconnect// 大厅客户端断开连接

OnLobbyStartClient// 大厅开始客户端

OnLobbyStopClient// 大厅停止客户端

OnLobbyClientSceneChanged// 大厅客户端场景切换

OnLobbyClientAddPlayerFailed// 大厅客户端添加玩家失败

All of the aboveclient functions have empty default implementations.

以上所有客户端函数都有默认空的实现。

Lobby Player Objects

大厅玩家对象

There are twokinds of player objects - each which has a prefab slot in theNetworkLobbyManager. The slots can be seen in this screenshot:

有两种类型的玩家对象 – NetworkLobbyManager(网络大厅管理器)中这两种预制的槽都有。可以在下面截图中看到:

Unity网络教程翻译(三)多人游戏大厅_第1张图片

The LobbyPlayeris created from the LobbyPlayerPrefab when a player joins the lobby:

当一个玩家加入大厅时会由LobbyPlayerPrefab(大厅玩家预制)创建LobbyPlayer(大厅玩家):

One LobbyPlayerfor each player

Created whenclient connects, or player is added

Exists untilclient disconnects

Holds the readyflag for this player for the lobby

Handles commandswhile in the lobby

Add user scriptsto this prefab to hold game-specific player data

This prefab musthave a NetworkLobbyPlayer component

每个玩家都会有一个大厅玩家

客户端连接或者玩家加入时创建

直到客户端断开连接否则会一直存在

为玩家保存大厅的准备状态

在大厅中时,处理在大厅中的命令

在这个预制上添加用户自定义脚本来保存玩家特有的游戏数据

这个预制必须有一个NetworkLobbyPlayer组件

Minimum Players

最少玩家下限

The “MinimumPlayers” field represents the minimum number of “Ready” players in the Lobby tostart 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.

“Minimum Players”(最少玩家下限)代表大厅开启一个游戏需要最少进行了准备的玩家的数量。如果连接的客户端的数量超过了最少玩家下限,当所有连接的客户端都变成准备后,游戏将会开始。

For example if“Minimum Players” is set to 2:

例如” Minimum Players“(最少玩家下限)设置为2

Start oneinstance of the game and start Host. Then in game Lobby UI press “Start” foryour player. You will still be in Lobby mode because minimum number of Readyplayers to start game is 2.

Start two moreinstances of the game and start Clients there. It doesn’t matter that “MinimumPlayers” set to 2. Wait for all - 3 in this case - connected players to becomeReady.

Press “Start” inLobby 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 Gamemode.

开启一个游戏主机,然后在大厅UI上点击”Start“,你将会还在大厅中,因为至少需要2个玩家进入准备状态才能开始游戏。再运行2个游戏客户端,” Minimum Players “设置为2是没关系的。等所有的三个玩家都变成准备状态,一个玩家在在大厅UI上点击开始。两个玩家准备,此时仍然在大厅模式下。最后一个玩家点击开始,现在所有玩家进入游戏模式。

GamePlayer

游戏玩家

The GamePlayer iscreated from the GamePlayerPrefab when the game starts:

在游戏开始的时候会根据GamePlayerPrefab(游戏玩家预制)创建GamePlayer(游戏玩家):

One GamePlayerfor each player

Created when gamescene is started

Destroyed whenre-entering lobby

Handles commandswhile in the game

This prefab musthave a NetworkIdentity component

每个玩家都有一个GamePlayer(游戏玩家)

在游戏场景开始的时候创建

返回大厅的时候销毁

在游戏进行过程中控制命令

这个预制必须有一个NetworkIdentify(联网×××)组件

The NetworkLobbyPlayer componentis used for LobbyPlayer objects. It supplies some virtual function callbacksthat can be used for custom lobby behavior

NetowrkLobbyPlayer(联网大厅玩家)组件是用于LobbyPlayer(大厅玩家)对象上的。它提供了一些虚函数回调来用于自定义大厅行为。

    public virtual void OnClientEnterLobby();// 客户端进入大厅

    public virtual void OnClientExitLobby(); //客户端退出大厅

    public virtual void OnClientReady(boolreadyState); // 客户端准备(是否准备)

The function OnClientEnterLobby iscalled on the client when the game enters the lobby. This happens when thelobby scene starts for the first time, and also when returning to the lobbyfrom the game-play scene.

当进入大厅时,OnClientEnterLobby回调会在客户端被调用。这个事件会在大厅场景第一次运行和从游戏运行场景返回大厅时发生。

The function OnClientExitLobby iscalled on the client when the game exists the lobby. This happens whenswitching to the game-play scene.

当退出大厅的时候,将会在客户端调用OnClientExitLobby回调。这个事件发生在切换到游戏场景中。

The function OnClientReady iscalled on the client when the ready state of that player changes.

当玩家改变准备状态的时候,OnClientReady会在客户端被回调。

Adding the Lobby to a Game

为游戏添加一个大厅

Process foradding a NetworkLobby to a multiplayer game (without using themultiplayer-lobby asset package):

为一个联网游戏添加NetworkLobby(联网大厅)的过程(在没有使用multplayer-lobby资源包的情况下):

Create a newlobby scene

Add the scene tothe build settings, as the first scene

Create a new gameobject in the new scene, rename it to LobbyManager

Add theNetworkLobbyManager component to the LobbyManager object

Add theNetworkManagerHUD component to the LobbyManager object

Open theinspector for the NetworkLobbyManager component

Set theLobbyScene slot of the NetworkLobbyManger to the scene that contains theLobbyManager object

Set the PlaySceneslot of the NetworkLobbyManager to the main game-play scene for the game

Create a newgameObject and rename it to LobbyPlayer

Add theNetworkLobbyPlayer component to the LobbyPlayer

Create a prefabfor the LobbyPlayer and delete the instance from the scene

Set theLobbyPlayerPrefab slot to the LobbyPlayer prefab

Set theGamePlayerPrefab slot to the prefab for the player in the main game

Save the scene.

Run the game

创建一个新的大厅窗井

将这个场景添加到Build Setting(打包设置)的场景列表中,并且将其顺序改为1(拖拽到最上方)

在大厅场景中创建一个新游戏对象,重命名为LobbyManager(大厅管理器)

LobbyManager(大厅管理器)对象上添加NetworkLobbyManager(联网大厅管理器)组件

LobbyManager(大厅管理器)对象上添加NetworkManagerHUD(联网管理器导航)组件

inspector(审查)视图中打开NetworkLobbyManager(联网大厅管理器)组件

将带有LobbyManager(大厅管理器)对象的大厅场景设置到LobbyScene(大厅场景)槽当中

将游戏主逻辑的游戏场景设置到NetworkLobbyManagerPlayScene(游戏场景)槽中

创建一个新的游戏对象并且将其重命名为LobbyPlayer(大厅玩家)

为其添加NetworkLobbyPlayer组件

LobbyPlayer制作成一个预制,并且删除掉场景中的LobbyPlayer对象

将刚才生成的大厅玩家预制拖到NetworkLobbyManagerLobbyPlayerPrefab槽上

将游戏中的玩家预制设置到NetorkLobbyManagerGamePlayerPrefab槽上

保存场景

运行游戏

This version ofthe NetworkLobbyManager uses the OnGUI user interface like the NetworkManagerHUD.For a better user interface use the multiplayer-lobby asset package.

NetworkLobbyManager版的用户界面是使用类似NetworkManagerHUD(联网导航)的OnGUI的界面。想要使用更好的用户界面请使用multiplayer-lobby资源包。

TheNetworkLobbyManager has many virtual function callbacks that can be used forcustom lobby behaviour. Most important is OnLobbyServerSceneLoadedForPlayerwhichis called on the server for each player as they transition from the lobby tothe main game. This is the ideal place to apply settings from the lobby to theplayer’s game-play object.

NetworkLobbyManager有很多回调虚函数可以用来自定义大厅行为。其中最重要的是当每个玩家从大厅切换到游戏时调用的OnLobbyServerSceneLoadedForPlayer函数。这里是将大厅的配置提交到游戏对象中最理想的位置。

// for users to apply settings from their lobby player object totheir in-game player object
// 将大厅玩家的配置提交给游戏玩家
    public override boolOnLobbyServerSceneLoadedForPlayer(GameObject lobbyPlayer, GameObject gamePlayer)
    {
        var cc =lobbyPlayer.GetComponent();
        var player =gamePlayer.GetComponent();
        player.myColor = cc.myColor;
        return true;
    }

Sample Project

示例项目

There is a sampleproject on the Unity asset store that uses the NetworkLobbyManager and providesa GUI for the lobby. This can be used as a starting point for making amultiplayer game with a lobby.

Lobby SampleProject

这里是Unity资源商店中一个使用了NetworkLobbyManager并且提供了GUI的简单示例。这个可以作为一个在线多人游戏大厅的入门。

简单大厅项目