查看: 1812|回复: 1
打印 上一主题 下一主题

[Unity 组件参考手册]桌面:网络参考指南之远程过程调用的细节

[复制链接]
.    

3797

主题

11

听众

5万

积分

首席设计师

Rank: 8Rank: 8

纳金币
32328
精华
41

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

跳转到指定楼层
楼主
发表于 2013-2-19 17:02:20 |只看该作者 |倒序浏览
Remote Procedure Calls (RPCs) let you call functions on a remote machine. It is similar to calling a normal function and almost as easy, but there are some important differences to understand.远程过程调用(RPCs)能让你调用远程机器上的函数。和调用普通函数非常相似,但有几处重要的差别必须清楚。    An RPC call can have as many parameters as you like. All parameters will of course be sent over the network. This makes the bandwidth increase with the more parameters you send. Therefore, you should try to minimize your parameters as much as possible.
    一个远程过程调用可以拥有许多参数。当然所有参数都通过网络传输。带参数传输会增加许多带宽开销。你需要尽可能的减少你的参数信息。
    You need to determine who will receive the RPC that is being sent out. There are several RPC call modes, that cover all common use cases. You can easily invoke the RPC function on everyone, on only the server, everyone but yourself or a specific client/player.
    你需要处理谁接收你传出的RPC。有几种RPC调用模式可以选择。几乎包括了所有可能的情况,你能轻易的执行在任何位置的RPC函数,不止是包括在服务器上,也包括你自己所在位置或者某个特别的客户端上。 RPC calls are usually used to&nbsp***cute some event on all clients in the game or pass event information specifically between two parties, but you can be creative with it and use it however you want to. For example, a server for a game which only starts after 4 clients have connected could send an RPC call to all clients as soon as the forth one connects, thus starting the game. A client could send RPC calls to everyone to signal that he picked up an item. A server could send an RPC to only a particular client to initialize him right after he connects, for example, to give him his player number, spawn location, team color, etc. A client could in turn send an RPC only to the server to specify his starting options, like which color he prefers or what items he has bought.RPC调用常常被用在执行一个需要在所有客户端上同时生效的事件或者在游戏的两个部分之间传递特殊的事件信息,你可以创建RPC然后再你希望的任何时候执行这个RPC.例如,一个游戏的服务器在连接4个客户端而且仅当4个客户端后会发出一个RPC调用到所有客户端,开始游戏。一个客户端能发送 一个RPC调用给注意到他捡起某件物品的每一个客户端。一个服务器也能发送一个RPC调用给一个刚连接到服务器的客户端启动初始化,可以是给客户端它的玩家编号,定位,队伍颜色,等等。一个客户端可以次序以RPC的方式发送它的诸如喜欢的颜色,它携带的物品等启动配置给服务器。
Using RPCs 使用RPCsThere are two steps involved in setting up RPC calls: declaring the function you want to call and invoking it. When declaring a function you have to prefix it with an RPC attribute.执行RPC调用前需要完成两步设定:首先声明你想要调用执行的函数。然后使用一个RPC前缀来修饰这个调用声明。The following code declares an RPC function.接面的代码声明了一个RPC函数。// All RPC calls need the @RPC attribute!
// 任何RPC调用都需要一个@RPC标签
@RPC
function PrintText (text : String)
{
    Debug.Log(text);
}Once you have declared an RPC call you can invoke it. All network communication goes through the Network View attached to the same GameObject as the script. In other words, you need to attach the scripts containing the RPCs functions to a GameObject that contains a Network View Component.一旦声明了一个RPC调用,你就可以执行它。所有网络通信通过绑定在同一个游戏对象上的Network View组件负责传递。换句话说,你只需要将包含RPCs函数的脚本绑定在一个包含NetworkView组件的游戏对象上就可以使用RPC。You can use the following variable types as parameters in RPCs:在RPC中你可以使用如下变量类型作为参数:    int 整数
    float 浮点数
    string 字符串
    NetworkPlayer 网络玩家
    NetworkViewID 网络视图ID
    Vector3 三维向量
    Quaternion 四元数The following code invokes an RPC function.接下来的代码演示如何执行一个RPC函数。networkView.RPC ("rintText", RPCMode.All, "Hello world");This code will use the attached Network View to invoke all "rintText()" functions in any scripts attached to the same GameObject.这段代码使用绑定的NetworkView来执行所有绑定在同一个游戏对象上的脚本组件中的"rintText()"RPC函数。The first parameter of RPC() is the name of the function to be invoked, the second determines who will have this function invoked. In this case we invoke the RPC call on everyone who is connected to the server but did not tell him to buffer the call for clients which connect later.RPC的第一个参数是被执行的函数的名字。第二个参数据决定在哪一个位置执行该函数。在这个例子中,我们会在所有连接到这个服务器的客户端中执行这个RPC调用但不会为后续连接上的客户端缓存这个调用。All the following parameters are the parameters that will be passed to the RPC function and be sent across the network. In this case, "Hello World" will be sent as a parameter and be passed as the text parameter in the PrintText function.然后接下来的参数都会通过网络传递给RPC函数执行方作为函数参数,在这个例子中,"Hello, world"会作为一个参数被传递,并做为PrintText函数的文本参数。You can also get hold of an extra internal parameter, a NetworkMessageInfo s***ct which holds details like who sent the RPC. You do not need to specifically pass in this information, so the PrintText function shown above will be invoked exactly the same but can be declared as:你还可以添加一个额外的保持了谁发送这个RPC的细节的NetworkMessageInfo结构的内部参数,你不需要在调用时特别的指明,因此PrintText函数可以修改为如下形式而执行过程依然和上述代码相同@RPC
function PrintText (text : String, info : NetworkMessageInfo)
{
    Debug.Log(text + " from " + info.sender);
}As already mentioned, in order for RPC function to work in a script, a Network View must exist on the GameObject which contains the script. The Network View's State Synchronization can be set to Off, unless you are using State Synchronization with the same Network View.前面提到过,为了RPC函数能在函数组件中工作,一个NetworkView组件必须同时存在于包含脚本组件的游戏对象中。NetworkView组件的状态同步设定可以被设置为关闭,除非你希望通过同一个NetworkView执行状态同步功能。
RPC Buffer (RPC缓冲区)RPC calls can also be buffered. Buffered RPC calls are stored up and&nbsp***cuted in order for each new connecting client. They can be useful for making the process of a player joining an existing game session easier. A common scenario is that every player who joins a game should first load a specific level. Thus you would send an RPC function to everyone and then also set it to be buffered. By buffering it, whenever a new player joins the game, it will automatically be sent to the player. This way you don't need to track players joining and manually send load level RPCs specifically to the player.RPC调用可以被缓冲。缓冲的RPC调用被存储起来,然后发送给每一个新连接上的客户端执行。这会让参加一个已经开始的游戏的客户端的开发过程变得更容易。还有一个通常会遇到的情况是每一个加入游戏的客户端首先都会加载一个特定的关卡。你可以发送一个RPC函数给每一个客户端,然后缓存这个调用,然后当一个新客户端加入游戏时,它会自动的被发送到客户端。这使你不需要追踪加入游戏的客户端,不需要特别为某个客户端人工发送读取关卡的RPC.Unity also has some functionality to remove calls from the RPC buffer. In the load level example, if the game has progressed through multiple levels, all you really care about is having a new player join the current level. In this case, you would remove any previously added Load Level RPC calls from the RPC buffer.Unity也有功能能去除RPC的缓存。在读取关卡的例子中,如果游戏已经进行了多个关卡,所有你必须关心的是使一个新玩家加入当前的关卡,为了解决这个问题,你也许会从RPC缓存中移除所有前面加载关卡的 RPC调用。 【来源:互联网】
更多精彩教程,尽在web3D纳金网http://www.narkii.com/college/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

nts    

3

主题

1

听众

743

积分

初级设计师

Rank: 3Rank: 3

纳金币
7
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2013-10-20 12:58:14 |只看该作者
网络参考指南之远程过程调用的细节
回复

使用道具 举报

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

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-14 07:38 , Processed in 0.064063 second(s), 30 queries .

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

© 2008-2019 Narkii Inc.

回顶部