标题: Unity进行网络实例化 [打印本页] 作者: 王者再临 时间: 2014-4-30 23:20 标题: Unity进行网络实例化 Network Instantiate lets you Instantiate a prefab on all clients in a very easy to use way. When a player calls Network.Instantiate(), they control any Input commands on that GameObject. All the setup and handling is performed under the hood, so all you need to do to assign control is make each player call Network.Instantiate() on the GameObject they should control.
Instantiating prefabs is one of the most important concepts in Unity. Network Instantiate is an extension to it, which lets you with a single function call instantiate an object across all machines on the network.
The argument list for Network.Instantiate() is as follows:
Network.Instantiate()的参数如下:
static function Instantiate (prefab : Object, position : Vector3, rotation : Quaternion, group : int) : Object
Like Instantiate the first three parameters describe the prefab to be instantiated and the position and rotation of where it should be placed. Additionally the group is passed in as an argument. Usually you just pass in 0 as the group. The group number allows you to group together network messages which allows you to filter them if so desired.
Behind the scenes, Network Instantiate is simply a convenience function built around RPC calls. It serializes an identifier of the prefab that is to be instantiated, serializes all allocated NetworkViewID's, serializes the position, and send it across the network using an RPC call. Network.Instantiate() will always buffer the Instantiate call in the RPC buffer, this also means it can be removed from the buffer in the same way as other RPC calls.
In essence this is a regular instantiation that is performed on all clients and automatically buffered. Players that join after the instantiation was performed will also receive the Instantiate call.
本质上来说这是一个在所有客户端执行并自动缓冲的标准实例化。在实例化后加入的玩家也会收到实例化调用并执行。
Communication Groups 通信组
Communication groups are important to differentiate communication and disable receiving
nding to some clients.
通信组对于有区别的通信和取消一些客户端的发送接收操作有重要的作用。
For example, two connected players might be in separate room without being able to meet. Thus there is no reason to transfer state of the two player avatars and depending on your situation. You could have one group for game data and one for chat communication. The game data group might be blocked when loading a level but the chat group kept open so players can communicate.