【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 2877|回复: 0

Network View网络视图组件

[复制链接]
发表于 2014-2-16 21:44:11 | 显示全部楼层 |阅读模式
本帖最后由 夜行的猫仔 于 2014-2-17 09:16 编辑

Network View

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.

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
The type of State Synchronization used by this Network View
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()中返回数据来实现。

  1. function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo) {
  2.         var horizontalInput : float = Input.GetAxis ("Horizontal");
  3.         stream.Serialize (horizontalInput);
  4. }
复制代码

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属性来实现。

  1. function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo) {
  2.         var horizontalInput : float = 0.0;
  3.         if (stream.isWriting) {
  4.                 // Sending
  5.                 horizontalInput = Input.GetAxis ("Horizontal");
  6.                 stream.Serialize (horizontalInput);
  7.         } else {

  8.                 // Receiving
  9.                 stream.Serialize (horizontalInput);
  10.                 // ... do something meaningful with the received variable
  11.         }
  12. }
复制代码

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()来执行远程调用操作。

  1. var playerBullet : GameObject;

  2. function Update () {
  3.         if (Input.GetButtonDown ("Fire1")) {
  4.                 networkView.RPC ("PlayerFire", RPCMode.All);
  5.         }
  6. }

  7. @RPC
  8. function PlayerFire () {
  9.         Instantiate (playerBullet, playerBullet.transform.position, playerBullet.transform.rotation);
  10. }
复制代码
  • 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

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-11-23 18:44

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表