日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Unity网络教程翻译(三)多人游戏大厅

發布時間:2023/12/20 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity网络教程翻译(三)多人游戏大厅 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文:

Unity網絡教程翻譯(三)多人游戲大廳



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 for multiple 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(會調用ServerChangeScene(PlayScene))都有默認空的實現。

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(網絡大廳管理器)中這兩種預制的槽都有。可以在下面截圖中看到:

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函數。這里是將大廳中玩家的配置應用到游戲中的玩家對象的最理想的地方。

1 2 3 4 5 6 7 8 9 //?for?users?to?apply?settings?from?their?lobby?player?object?totheir?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; ????}


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的簡單示例。這個可以作為一個在線多人游戲大廳的入門。

簡單大廳項目


總結

以上是生活随笔為你收集整理的Unity网络教程翻译(三)多人游戏大厅的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。