包含11節(jié)視頻教程
從零基礎(chǔ)學(xué)習(xí)unity3d游戲引擎,簡單易學(xué)的視頻教程,讓你快速掌握unity3d,并喜歡上游戲開發(fā)的樂趣。
![]()
|
![]() Network Views are the gateway to creating networked multiplayer games in Unity. They are simple to use, but they are extremely powerful. For this reason, it is recommended that you understand the fundamental concepts behind networking before you start experimenting with Network Views. You can learn and discover the fundamental concepts in the Network Reference Guide. 網(wǎng)絡(luò)視圖是在Unity中創(chuàng)建網(wǎng)絡(luò)多人游戲的重要一步。易于使用,而且非常強(qiáng)大。在你開始練習(xí)使用網(wǎng)絡(luò)視圖之前你應(yīng)該更多了解網(wǎng)絡(luò)背后的基本概念。你能通過Network Reference Guide學(xué)到這些知識。
In order to use any networking capabilities, including State Synchronization or Remote Procedure Calls, your GameObject must have a Network View attached. 為了使用包括狀態(tài)同步或者遠(yuǎn)程過程調(diào)用等網(wǎng)絡(luò)功能,首先你必須創(chuàng)建一個綁定了網(wǎng)絡(luò)視圖組件的游戲?qū)ο蟆?/p> Properties 屬性
Details 細(xì)節(jié)When you add a Network View to a GameObject, you must decide two things 當(dāng)你添加了一個網(wǎng)絡(luò)視圖到游戲?qū)ο螅惚仨毻瓿蓛蓚步驟。
Choosing data to send 選擇數(shù)據(jù)來發(fā)送The Observed property of the Network View can contain a single Component. This can be a Transform, an Animation, a RigidBody, or a script. Whatever the Observed Component is, data about it will be sent across the network. You can select a Component from the drop-down, or you can drag any Component header directly to the variable. If you are not directly sending data, just using RPC calls, then you can turn off synchronization (no data directly sent) and nothing needs to be set as the Observed property. RPC calls just need a single network view present so you don't need to add a view specifically for RPC if a view is already present. 網(wǎng)絡(luò)視圖的觀察者屬性用來指定單個組件?梢允且粋變換、一個動畫、或是一個剛體、或者一個腳本?傊,觀察者組件是將會通過網(wǎng)絡(luò)發(fā)送的數(shù)據(jù)。你可以通過下拉菜單選擇的一個組件,或者你直接拖動某個組件到該網(wǎng)絡(luò)視圖的觀察者屬性為其賦值。如果你不直接發(fā)送數(shù)據(jù),只是使用RPC調(diào)用,那你可以關(guān)閉同步(沒有數(shù)據(jù)會被直接發(fā)送)并且設(shè)置觀察者屬性為空。RPC調(diào)用只需要有一個網(wǎng)絡(luò)視圖存在,因此如果有一個視圖已經(jīng)存在你不需要再添加一個特殊的視圖。 How to send the data 如何發(fā)送數(shù)據(jù)You have 2 options to send the data of the Observed Component: State Synchronization and Remote Procedure Calls. 你有兩種發(fā)送數(shù)據(jù)觀察者組件數(shù)據(jù)的選擇:狀態(tài)同步和遠(yuǎn)程過程調(diào)用。 To use State Synchronization, set State Synchronization of the Network View to Reliable Delta Compressed or Unreliable. The data of the Observed Component will now be sent across the network automatically. 當(dāng)設(shè)置網(wǎng)絡(luò)視圖的狀態(tài)同步為可靠的延遲的加密的模式或者不可靠的模式。觀察者屬性所關(guān)聯(lián)的數(shù)據(jù)會自動通過網(wǎng)絡(luò)發(fā)送。 Reliable Delta Compressed is ordered. Packets are always received in the order they were sent. If a packet is dropped, that packet will be re-sent. All later packets are queued up until the earlier packet is received. Only the difference between the last transmissions values and the current values are sent and nothing is sent if there is no difference. RDC是有序的。包總是以他們發(fā)送的次序被接收。如果包丟失了,包會被重發(fā)。后續(xù)的所有包會被緩存起來直到丟失的包被接收到。只有最后傳輸?shù)闹岛彤?dāng)前的值之間發(fā)生改變的值會被傳送。如果沒有改變沒有值會被傳送。 If it is observing a Script, you must explicitly Serialize data within the script. You do this within the OnSerializeNetworkView() function. 如果被觀察的是一個腳本組件,你必須明確的指出和腳本相關(guān)聯(lián)的數(shù)據(jù),你可以通過在OnSerializeNetworkView()中返回數(shù)據(jù)來實現(xiàn)。
The above function always writes (an update from the stream) into horizontalInput, when receiving an update and reads from the variable writing into the stream otherwise. If you want to do different things when receiving updates or sending you can use the isWriting attribute of the BitStream class. 上述函數(shù)中的代碼總是寫入(或者是從流中提取)到horizontalInput,為了接收到一個更新或是讀出一個正寫入流中的變量。如果你想在接收更新或者發(fā)送時能做些別的事情,你能通過使用BitStream上的isWriting屬性來實現(xiàn)。
OnSerializeNetworkView is called according to the sendRate specified in the network manager project settings. By default this is 15 times per second. OnSerializeNetworkView根據(jù)在網(wǎng)絡(luò)管理器工程設(shè)定里的sendRate(發(fā)送速率)進(jìn)行調(diào)用。默認(rèn)的是每秒鐘調(diào)用15次。 If you want to use Remote Procedure Calls in your script all you need is a NetworkView component present in the same GameObject the script is attached to. The NetworkView can be used for something else, or in case it's only used for sending RPCs it can have no script observed and state synchronization turned off. The function which is to be callable from the network must have the @RPC attribute. Now, from any script attached to the same GameObject, you call networkView.RPC() to execute the Remote Procedure Call. 如果你想在腳本中使用RPC你所需要做的是在腳本組件所在的游戲?qū)ο笊辖壎ㄒ粋網(wǎng)絡(luò)視圖組件對象。網(wǎng)絡(luò)組件還能被用作其他用途,或者只用于發(fā)送RPC, 網(wǎng)絡(luò)組件上不綁定任何腳本組件并且狀態(tài)同步選項被關(guān)閉?梢酝ㄟ^網(wǎng)絡(luò)組件進(jìn)行訪問的RPC函數(shù)必須具備@RPC屬性,F(xiàn)在,從任何綁定在游戲?qū)ο蟮哪_本組件中都可以通過調(diào)用networkView.RPC()來執(zhí)行遠(yuǎn)程調(diào)用操作。
RPCs are transmitted reliably and ordered. For more information about RPCs, see the RPC Details page. RPC被可靠的傳輸。在RPC Details可以獲得更多RPC的詳細(xì)細(xì)節(jié)。 Hints 提示
贊0 踩0 |
未知用戶
2005-2025 朱峰社區(qū) 版權(quán)所有 遼ICP備2021001865號-1
2005-2025 ZhuFeng Community All Rights Reserved
VIP