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.
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.
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.
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.
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.
??? 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.
This version ofthe NetworkLobbyManager uses the OnGUI user interface like the?NetworkManagerHUD.For a better user interface use the multiplayer-lobby asset package.
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.
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.