NetworkView.RPC 远程过程调用

function RPC (name : string, mode : RPCMode, params args : object[]) : void

Description描述

Call a RPC function on all connected peers.

在所有连接端调用一个RPC函数。

The called function must have the @RPC tag set ([RPC] for C Sharp code). A NetworkView must be attached to the GameObject where the RPC function is being called. It doesn't matter if the NetworkView is being used for something else or just for the RPC function. If it is just for the RPC function, state synchronization should be turned off and the observed property can be set to none. RPC function names should be unique accross the scene, if two RPC functions in different scripts have the same name only one of them is called when RPC is invoked. RPC calls are always guaranteed to be executed in the same order as they are sent. The communication group set for the network view, with NetworkView.group, is used for the RPC call. To get information on the RPC itelf, you can add a NetworkMessageInfo parameter to the function declaration which will automatically contain the information. You don't need to change the way you call the RPC function when you do this. For more information see the RPC section of the manual. Valid RPC parameters are int, float, string, NetworkPlayer, NetworkViewID, Vector3 and Quaternion.

调用的函数必须有@RPC标识([RPC]用于C#)。NetworkView必须附加到GameObject上,RPC函数才能够被调用。NetworkView用于其他地方或者是仅用于RPC函数是没有关系的。如果仅用于RPC函数,stateSynchronization应该被关掉,并且Ovserved属性设置为none,在整个场景中RPC函数的名称应该是唯一的,如果不同脚本的两个RPC函数具有相同的名称,仅有一个会被调用。RPC调用总是确保执行的顺序与他们调用的顺序相同。用NetworkView.group为NetworkView设置的通信组,被用于RPC调用。为了获取RPC自身的信息,可以添加一个NetworkMessageInfo参数到函数声明中,它将自动包含这个信息。这样的做的时候你不需要改变调用RPC函数的方式,可以参考手册中的RPC部分以便获取更多关于RPC的信息。可用的RPC参数为int,float,string,NetworkPlayer,NetworkViewID,Vector3和Quaternion。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Transform cubePrefab;
	void OnGUI() {
		if (GUILayout.Button("SpawnBox")) {
			NetworkViewID viewID = Network.AllocateViewID();
			networkView.RPC("SpawnBox", RPCMode.AllBuffered, viewID, transform.position);
		}
	}
	[RPC]
	void SpawnBox(NetworkViewID viewID, Vector3 location) {
		Transform clone;
		clone = Instantiate(cubePrefab, location, Quaternion.identity) as Transform;
		NetworkView nView;
		nView = clone.GetComponent<NetworkView>();
		nView.viewID = viewID;
	}
}

• function RPC (name : string, target : NetworkPlayer, params args : object[]) : void

Description描述

Call a RPC function on a specific player

在一个特定的玩家,调用一个RPC函数。

最后修改:2011年3月30日 Wednesday 17:15

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。