查看: 3152|回复: 0

[Unity 组件参考手册]组件:网络组之网络视图

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

活跃会员 优秀版主 荣誉管理 论坛元老

发表于 2013-2-3 17:36:12 |显示全部楼层
This group contains all the Components that relate to Networked Multiplayer games.该组包含了相关网络多人游戏的所有组件。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.网络视图是在Unity中创建网络多人游戏的重要一步。易于使用,而且非常强大。在你开始练习使用网络视图之前你应该更多了解网络背后的基本概念。你能通过Network Reference Guide学到这些知识。
The Network View Inspector 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
    如果你只使用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&nbsp***ntime
    这个选项将决定是在视图中预先创建或是实时创建。——————————————————————————————————————【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&nbsp***cute the Remote Procedure Call.如果你想在脚本中使用RPC你所需要做的是在脚本组件所在的游戏对象上绑定一个网络视图组件对象。网络组件还能被用作其他用途,或者只用于发送RPC, 网络组件上不绑定任何脚本组件并且状态同步选项被关闭。可以通过网络组件进行访问的RPC函数必须具备@RPC属性。现在,从任何绑定在游戏对象的脚本组件中都可以通过调用networkView.RPC()来执行远程调用操作。var playerBullet : GameObject;function Update () {
if (Input.GetButtonDown ("Fire1")) {
  networkView.RPC ("layerFire", 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()。 【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-3-29 20:07 , Processed in 0.148067 second(s), 34 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部