Network.AllocateViewID 分配网络视图ID

static function AllocateViewID () : NetworkViewID

Description描述

Query for the next available network view ID number and allocate it (reserve).

查询下一个可用的网络视图ID号并分配它(保留)。

This number can then be assigned to the network view of an instantiated object. The example below demonstrates a simple method to do this. Note that for this to work there must be a NetworkView attached to the object which has this script and it must have the script as its observed property. There must be a Cube prefab present also with a NetworkView which watches something (like the Transform of the Cube). The cubePrefab variable in the script must be set to that cube prefab. This is the simplest method of using AllocateViewID intelligently. This get more complicated if there were more than one NetworkView attached to the Cube which is to be instantiated.

这个数字可以被分配到一个实例化物体的网络视图。下面的例子演示了一个简单的方法来做到这一点。注意,为了使其可正常工作,必须有一个NetworkView附加到有这个脚本的物体,并这个脚本作为它的观察属性。必须有一个Cube预设,带有一个NetworkView监视某些东西(如Cube的Transform)。脚本中的cubePrefab变量必须设置为cube预设。使用智能的AllocateViewID是最简单的方法。如果有超过一个NetworkView附加在初始化的Cube上着将变得更复杂。

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;
	}
}
var cubePrefab : Transform;
function OnGUI () {
	if (GUILayout.Button("SpawnBox")) {
		var viewID = Network.AllocateViewID();
		networkView.RPC("SpawnBox",
            RPCMode.AllBuffered,
            viewID,
            transform.position);
	}
}

@RPC
function SpawnBox (viewID : NetworkViewID, location : Vector3) {
	// Instantate the prefab locally
	//实例化本地预设
	var clone : Transform;
	clone = Instantiate(cubePrefab, location, Quaternion.identity) as Transform;
	var nView : NetworkView;
	nView = clone.GetComponent(NetworkView);
	nView.viewID = viewID;
}
最后修改:2011年4月2日 Saturday 18:28

本脚本参考基于Unity 3.4.1f5

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