本帖最后由 夜行的猫仔 于 2014-2-17 09:16 编辑
Network ViewNetwork 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. Network View网络视图组件是一个通过网络共享数据的组件。
In order to use any networking capabilities, including State Synchronization or Remote Procedure Calls, your GameObject must have a Network View attached.
Properties | | State Synchronization | | Off | No State Synchronization will be used. This is the best option if you only want to send RPCs | Reliable Delta Compressed | The difference between the last state and the current state will be sent, if nothing has changed nothing will be sent. This mode is ordered. In the case of packet loss, the lost packet is re-sent automatically | Unreliable | The complete state will be sent. This uses more bandwidth, but the impact of packet loss is minimized | Observed | The Component data that will be sent across the network | View ID | The unique identifier for this Network View. These values are read-only in the Inspector | Scene ID | The number id of the Network View in this particular scene | Type | Either saved to the Scene or Allocated at runtime | 其中StateSynchronization:同步状态,通过网络视图组件的同步类型具有三个选项:
1.off:关闭如果只使用RPCs建议关闭。
2.Reliable Delta Compressed 可靠压缩模式,只发送游戏中变化的数据,如果没变化则不传递。如果传递丢包则重新发送。这个机制将会引起网络卡死。
3.unreliable 不可靠传递,丢包不重发。但是会发送所有的状态,消耗资源更多。
Oberver:变量组件的数据会在网络中传递。
View ID:视图编号
SceneID 当前视图中Network View的编号。
Type:类型--是事先创建好的还是实施创建的。
Details 细节When you add a Network View to a GameObject, you must decide two things 当你添加了一个网络视图到游戏对象,你必须完成两个步骤。 - What kind of data you want the Network View to send
你希望通过网络视图发送何种数据 - How you want to send that data
你希望以何种方式发送这些数据
Choosing data to send 选择数据来发送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. 网络视图的观察者属性用来指定单个组件。可以是一个变换、一个动画、或是一个刚体、或者一个脚本。总之,观察者组件是将会通过网络发送的数据。你可以通过下拉菜单选择的一个组件,或者你直接拖动某个组件到该网络视图的观察者属性为其赋值。如果你不直接发送数据,只是使用RPC调用,那你可以关闭同步(没有数据会被直接发送)并且设置观察者属性为空。RPC调用只需要有一个网络视图存在,因此如果有一个视图已经存在你不需要再添加一个特殊的视图。 How to send the data 如何发送数据You have 2 options to send the data of the Observed Component: State Synchronization and Remote Procedure Calls. 你有两种发送数据观察者组件数据的选择:状态同步和远程过程调用。 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. 当设置网络视图的状态同步为可靠的延迟的加密的模式或者不可靠的模式。观察者属性所关联的数据会自动通过网络发送。 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是有序的。包总是以他们发送的次序被接收。如果包丢失了,包会被重发。后续的所有包会被缓存起来直到丢失的包被接收到。只有最后传输的值和当前的值之间发生改变的值会被传送。如果没有改变没有值会被传送。 If it is observing a Script, you must explicitly Serialize data within the script. You do this within the OnSerializeNetworkView() function. 如果被观察的是一个脚本组件,你必须明确的指出和脚本相关联的数据,你可以通过在OnSerializeNetworkView()中返回数据来实现。 - function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo) {
- var horizontalInput : float = Input.GetAxis ("Horizontal");
- stream.Serialize (horizontalInput);
- }
复制代码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. 上述函数中的代码总是写入(或者是从流中提取)到horizontalInput,为了接收到一个更新或是读出一个正写入流中的变量。如果你想在接收更新或者发送时能做些别的事情,你能通过使用BitStream上的isWriting属性来实现。 - function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo) {
- var horizontalInput : float = 0.0;
- if (stream.isWriting) {
- // Sending
- horizontalInput = Input.GetAxis ("Horizontal");
- stream.Serialize (horizontalInput);
- } else {
- // Receiving
- stream.Serialize (horizontalInput);
- // ... do something meaningful with the received variable
- }
- }
复制代码OnSerializeNetworkView is called according to the sendRate specified in the network manager project settings. By default this is 15 times per second. OnSerializeNetworkView根据在网络管理器工程设定里的sendRate(发送速率)进行调用。默认的是每秒钟调用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你所需要做的是在脚本组件所在的游戏对象上绑定一个网络视图组件对象。网络组件还能被用作其他用途,或者只用于发送RPC, 网络组件上不绑定任何脚本组件并且状态同步选项被关闭。可以通过网络组件进行访问的RPC函数必须具备@RPC属性。现在,从任何绑定在游戏对象的脚本组件中都可以通过调用networkView.RPC()来执行远程调用操作。 - var playerBullet : GameObject;
- function Update () {
- if (Input.GetButtonDown ("Fire1")) {
- networkView.RPC ("PlayerFire", RPCMode.All);
- }
- }
- @RPC
- function PlayerFire () {
- Instantiate (playerBullet, playerBullet.transform.position, playerBullet.transform.rotation);
- }
复制代码RPCs are transmitted reliably and ordered. For more information about RPCs, see the RPC Details page. RPC被可靠的传输。在RPC Details可以获得更多RPC的详细细节。 Hints 提示
- Read through the Network Reference Guide if you're still unclear about how to use Network Views
通过阅读Network Reference Guide,如果你还清楚如何使用网络视图。 - State Synchronization does not need to be disabled to use Remote Procedure Calls
使用RPC不需要关闭状态同步 - If you have more than one Network View and want to call an RPC on a specific one, use GetComponents(NetworkView).RPC().
如果你想通过在多个网络视图中选择一个来完成RPC时,请使用GetComponents(NetworkView).RPC()。
Page last updated: 2013-07-12 |