Unity+ Photon服務器實時對戰遊戲——主服務器和大廳(四)

Master Server And Lobby 主服務器和大廳

PUN always uses a master server and one or more game servers. The master server manages currently running games on the various game servers and will provide a game server address when you join or create a room. PUN (the client) automatically switches to that game server.

PUN總是使用一個主服務器和一個或多個遊戲服務器。 主服務器管理當前運行遊戲的各種遊戲服務器和將提供一個遊戲服務器地址當你加入或創建一個房間。 PUN(客戶端),它會自動切換到遊戲服務器。

Individual matches are known as Rooms. They are independent of each other and identified by name. Rooms are grouped into one or multiple lobbies. Lobbies are an optional part in matchmaking. If you don't use custom lobbies explicitly, PUN will use a single lobby for all rooms.

個人比賽房間。 他們是相互獨立的和確定的名字。 房間都分成一個或多個遊說團體。 大堂是一個可選的相親。 如果你不使用自定義顯式地遊說,PUN將爲所有房間使用一個大廳。

By default, PUN will join the default lobby after connecting. This lobby sends a list of existing rooms to the client, so the player can pick a room (by name or some properties listed). Access the current list by using PhotonNetwork.GetRoomList(). The lists is updated in intervals to keep traffic low.

默認情況下,PUN將加入連接後默認的遊說。 這個大廳發送客戶端現有的房間列表,所以玩家可以選擇一個房間(按名稱或列出一些屬性)。 通過使用PhotonNetwork.GetRoomList訪問當前列表()。 的列表更新間隔保持低流量。

Clients don't have to join a lobby to join or create rooms. If you don't want to show a list of rooms in your client, set PhotonNetwork.autoJoinLobby = false before you connect and your clients will skip the lobby.

客戶不需要加入一個遊說加入或創建房間。 如果你不想顯示一個房間在您的客戶端列表,設置PhotonNetwork。 autoJoinLobby = false之前你連接,你的客戶會跳過大廳。

You can use more than one lobby to organize room-lists as needed for your game. PhotonNetwork.JoinLobby is the method to join a specific lobby. You can make them up on the client side - the server will keep track of them. As long as name and type are the same, the TypedLobby will be the same for all clients, too.

您可以使用一個以上的遊說組織room-lists作爲你的遊戲需要。 PhotonNetwork。 JoinLobby方法加入特定的遊說。 你可以讓他們在客戶端-服務器將跟蹤他們。 只要名稱和類型是相同的,所有客戶的TypedLobby將是相同的。

A client is always just in one lobby and while being in a lobby, creating a room will relate to this lobby, too. Multiple lobbies mean the clients get shorter rooms lists, which is good. There is no limit to the rooms lists.

客戶總是在一個遊說團體在遊說,創建一個房間也會與這個大廳。 多個遊說團體意味着客戶端變短的房間列表,這是好的。 是沒有限制的房間列表。

A parameter in JoinRoom, JoinRandomRoom and CreateRoom enables you to select a lobby without joining it.

JoinRoom中的一個參數,JoinRandomRoom CreateRoom使您能夠選擇一個遊說沒有加入它。

Players won't notice each other in the Lobby and can't send data (to prevent issues when it's getting crowded).

玩家不會注意到彼此在大堂,不能發送數據(防止問題的時候擁擠)。

The servers are all run on dedicated machines - there is no such thing as player-hosted ‘servers’. You don’t have to bother remembering about the server organization though, as the API all hides this for you.

服務器都運行在專用的機器——不存在player-hosted“服務器”。 你不必費心記住關於服務器組織,API都隱藏這對你。
PhotonNetwork.ConnectUsingSettings(“v1.0“);

The code above is required to make use of any PhotonNetwork features. It sets your client’s game version and uses the setup-wizard’s config (stored in: PhotonServerSettings). The wizard can also be used when you host Photon yourself. Alternatively, use Connect() and you can ignore the PhotonServerSettings file.

上面的代碼應使用任何PhotonNetwork特性。 它集客戶的遊戲版本和使用安裝嚮導的配置(存儲在:PhotonServerSettings)。 嚮導還可以使用當你自己主機Photon。 另外,使用Connect()和可以忽略PhotonServerSettings文件。

Versioning 版本控制

The loadbalancing logic for Photon uses your appID to separate your players from anyone else’s. The same is done by game version, which separates players with a new client from those with older clients. As we can’t guarantee that different Photon Unity Networking versions are compatible with each other, we add the PUN version to your game’s version before sending it (since PUN v1.7).

負載平衡Photon邏輯使用appID分離你從別人的玩家。 同樣是通過遊戲版本,將玩家與一個新客戶的老客戶。 我們不能保證不同Photon相互兼容Unity網絡版本,我們將雙關語版本添加到您的遊戲版本,然後再把它寄(因爲雙關語v1.7)。

Creating and Joining Games 創建和加入遊戲

Next, you’ll want to join or create a room. The following code showcases some required functions: 接下來,您將想要加入或創建一個房間。 下面的代碼展示了一些需要的功能:

//Join a room 加入房間
PhotonNetwork.JoinRoom(roomName);
//Create this room. 創建一個房間
PhotonNetwork.CreateRoom(roomName);
// Fails if it already exists and calls: OnPhotonCreateGameFailed 失敗,如果它已經存在並調用:OnPhotonCreateGameFailed
//Tries to join any random game: 試圖加入任何隨機的遊戲:
PhotonNetwork.JoinRandomRoom();
//Fails if there are no matching games: OnPhotonRandomJoinFailed

A list of currently running games is provided by the master server’s lobby. It can be joined like other rooms but only provides and updates the list of rooms. The PhotonNetwork plugin will automatically join the lobby after connecting. When you’re joining a room, the list will no longer update.

當前運行遊戲的列表是由主服務器的遊說。 它可以加入像其他房間,但只提供和更新房間列表。 PhotonNetwork插件會自動連接後加入遊說。 當你加入一個房間時,將不再更新列表。

To display the list of rooms (in a lobby): 顯示的房間列表

foreach (RoomInfo game in PhotonNetwork.GetRoomList())
{
GUILayout.Label(game.name + " " + game.playerCount + "/" + game.maxPlayers);
}

Alternatively, the game can use random matchmaking: It will try to join any room and fail if none has room for another player. In that case: Create a room without name and wait until other players join it randomly.

另外,遊戲可以使用隨機配對:它將嘗試加入任何房間,失敗如果沒有房間另一個玩家。 在這種情況下:創建一個沒有名字的房間,等待其他玩家加入隨機。

Advanced Matchmaking & Room Properties

Fully random matchmaking is not always something players enjoy. Sometimes you just want to play a certain map or just two versus two.

完全隨機配對並不總是玩家喜歡的事情。 有時候你只是想扮演一個特定的地圖或兩個和兩個。

In Photon Cloud and Loadbalancing, you can set arbitrary room properties and filter for those in JoinRandom.

在Photon雲和負載平衡,可以設置任意空間屬性和過濾的加入隨機。

Room Properties and the Lobby

Room properties are synced to all players in the room and can be useful to keep track of the current map, round, starttime, etc. They are handled as Hashtable with string keys. Preferably short keys.

房間屬性是同步到所有玩家的房間,可能是有用的跟蹤當前地圖,圓的、開始時間等。它們作爲散列表處理字符串鍵。 最好是短鍵

You can forward selected properties to the lobby, too. This makes them available for listing them and for random matchmaking, too. Not all room properties are interesting in the lobby, so you define the set of properties for the lobby on room creation.

你也可以選擇屬性轉發給大堂。 這使得它們用於清單,隨機配對,。 並不是所有房間屬性是有趣的大廳裏,所以你定義一組屬性的遊說創造空間。
Hashtable roomProps = new Hashtable() { { "map", 1 } };
string[] roomPropsInLobby = { "map""ai" };
RoomOptions roomOptions = new RoomOptions() { customRoomProperties = roomProps, customRoomPropertiesForLobby = roomPropsInLobby }
CreateRoom(roomName, roomOptions, TypedLobby.Default)

Note that "ai" is not a key in the room-properties yet. It won't show up in the lobby until it's set in the game via Room.SetCustomProperties(). When you change the values for "map" or "ai", they will be updated in the lobby with a short delay, too.

請注意,“人工智能”還沒有room-properties的關鍵。 它不會出現在大廳裏,直到它設置在遊戲中通過Room.SetCustomProperties()。 當你改變的值“地圖”或“人工智能”,他們將在大堂與短的延遲更新,。

Keep the list short to make sure performance doesn't suffer from loading the list.

保持短列表,以確保從加載列表中性能不受影響。

Filtering Room Properties in Join Random

過濾室中加入隨機屬性

In JoinRandom, you could pass a Hashtable with expected room properties and max player value. These work as filters when the server selects a "fitting" room for you.

在JoinRandom,你可以通過一個Hashtable預期空間屬性和馬克斯玩家的價值。 這些工作過濾器當服務器爲你選擇一個“合適”的房間。
Hashtable expectedCustomRoomProperties = new Hashtable() { { "map", 1 } };
JoinRandomRoom(expectedCustomRoomProperties, 4);

If you pass more filter properties, chances are lower that a room matches them. Better limit the options.

如果你通過更多的過濾特性,一個房間比賽的機會較低。 更好的限制選項。

Make sure you never filter for properties that are not known to the lobby (see above).

確保你永遠不會過濾屬性不知道大堂(見上圖)。

MonoBehaviour Callbacks 回調

PUN uses several callbacks to let your game know about state changes like “connected” or “joined a game”. All you have to do is implement the fitting method in any MonoBehaviour and it gets called when the event happens.

雙關使用多個回調,讓你的遊戲知道狀態改變像“連接”或“加入遊戲”。 所有你要做的就是實現擬合方法在任何MonoBehaviour事件發生時被調用。

To get a good overview of available callbacks, take a look at the class Photon.PunBehaviour. If you make your script a PunBehaviour (instead of a MonoBehaviour), you can override individual callbacks easily. If you begin to type "override", your coding IDE should provide you a list of callbacks, so they are easy to find while coding, too.

得到很好地概述可用的回調,看看Photon.PunBehaviour的類。 如果你使你的腳本PunBehaviour(而不是MonoBehaviour),您可以重寫個人容易回調。 如果你開始“覆蓋”類型,編碼IDE應該提供你一個回調函數的列表,所以它們很容易找到編碼時,。

This covers the basics of setting up game rooms. Next up is actual communication in games.

這涵蓋了基本的設置遊戲房間。 接下來是實際溝通在遊戲中。

Sending messages in rooms 發送消息在房間

Inside a room you are able to send network messages to other connected players. Furthermore you are able to send buffered messages that will also be sent to players that connect in the future (for spawning your player for instance).

在一個房間你可以發送網絡消息到其他玩家連接。 而且你能夠發送緩衝,也會將消息發送到玩家連接未來的(例如產卵你的玩家)。

Sending messages can be done using two methods. Either RPCs or by using the PhotonView property OnSerializePhotonView. There is more network interaction though. You can listen for callbacks for certain network events (e.g. OnPhotonInstantiate, OnPhotonPlayerConnected) and you can trigger some of these events (PhotonNetwork.Instantiate). Don’t worry if you’re confused by the last paragraph, next up we’ll explain for each of these subjects.

發送消息可以使用兩種方法。 rpc或通過使用OnSerializePhotonView PhotonView屬性。 有更多的網絡交互。 可以爲某些網絡監聽回調事件(例如OnPhotonInstantiate OnPhotonPlayerConnected)可以觸發一些事件(PhotonNetwork.Instantiate)。 不要擔心如果你困惑最後一段,接下來我們將解釋這些科目。

Using Groups in PUN 用組

Groups are not synchronized when they are changed on any PhotonView. It's up to the developer to keep photonviews in the same groups on all clients, if that's needed. Using different group numbers for the same photonview on several clients will cause some inconsistent behaviour.組不同步時在任何PhotonView改變。 由開發人員保持photonviews在同一組對所有客戶,如果這是必要的。 使用不同組數據相同的photonview幾個客戶會導致一些不一致的行爲。

Some network messages are checked for their receiver group at the receiver side only, namely: 一些網絡消息檢查接收器組在接收機端,即:

  • RPCS that are targeted to a single player (or MasterClient) rpc是針對一個單一的玩家(或MasterClient)
  • RPCS that are buffered (AllBuffered/OthersBuffered). rpc緩衝(AllBuffered / OthersBuffered)。
  • This includes PhotonNetwork.Instantiate (as it is buffered). 這包括PhotonNetwork。 實例化(緩衝)。

Technical reason for this: the photon server only supports interestgroups for messages that are not cached and are not targetted at sepcific actor(s). This might change in the future.

技術原因:Photon服務器只支持服務的消息不緩存,不針對摘要演員(s)。 這在未來可能會改變。

PhotonView

PhotonView is a script component that is used to send messages (RPCs and OnSerializePhotonView). You need to attach the PhotonView to your games gameobjects. Note that the PhotonView is very similar to Unity’s NetworkView.

PhotonView是一個腳本組件,用於發送消息(rpc和OnSerializePhotonView)。 你需要把PhotonView gameobjects遊戲。 注意,PhotonView非常類似於Unity的NetworkView。

At all times, you need at least one PhotonView in your game in order to send messages and optionally instantiate/allocate other PhotonViews.

在任何時候,你需要至少一個PhotonView在你的遊戲和其他可選地實例化/分配PhotonViews發送消息。

To add a PhotonView to a gameobject, simply select a gameobject and use: “Components/Miscellaneous/Photon View”.

添加一個PhotonView gameobject,簡單地選擇一個gameobject和使用:“組件/雜項/Photon視圖”。
doc-photonview.jpg
Photon View

Observe Transform 觀察變換

If you attach a Transform to a PhotonView’s Observe property, you can choose to sync Position, Rotation and Scale or a combination of those across the players. This can be a great help for prototyping or smaller games. Note: A change to any observed value will send out all observed values - not just the single value that’s changed. Also, updates are not smoothed or interpolated.

如果你連接一個變換PhotonView的觀察屬性,您可以選擇同步位置,旋轉和規模或那些在玩家的組合。 這是一個偉大的幫助原型或小遊戲。 注意:更改任何觀察到的值將發送所有觀測值——不僅僅是單值的改變。 同時,更新不平滑或竄改。

Observe MonoBehaviour

A PhotonView can be set to observe a MonoBehaviour. In this case, the script’s OnPhotonSerializeView method will be called. This method is called for writing an object’s state and for reading it, depending on whether the script is controlled by the local player.

一個MonoBehaviour PhotonView可以觀察。 在這種情況下,腳本OnPhotonSerializeView方法將被調用。 調用此方法爲寫作對象的狀態和閱讀它,取決於該腳本是由當地的玩家。

The simple code below shows how to add character state synchronization with just a few lines of code more:

下面的簡單的代碼顯示瞭如何添加角色狀態同步只有幾行代碼:
void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.isWriting)
{
//We own this player: send the others our data
stream.SendNext((int)controllerScript._characterState);
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
//Network player, receive data
controllerScript._characterState = (CharacterState)(int)stream.ReceiveNext();
correctPlayerPos = (Vector3)stream.ReceiveNext();
correctPlayerRot = (Quaternion)stream.ReceiveNext();
}
}

If you send something “ReliableDeltaCompressed”, make sure to always write data to the stream in the same order. If you write no data to the PhotonStream, the update is not sent. This can be useful in pauses. Now on, to yet another way to communicate: RPCs.

如果你發送一些“ReliableDeltaCompressed”,確保總是在相同的順序向流寫入數據。 如果你寫PhotonStream沒有數據,更新不發送。 這可能是有用的在停頓了一下。 現在,另一種方法來溝通:rpc。

發佈了151 篇原創文章 · 獲贊 89 · 訪問量 79萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章