返回介绍

Network.AllocateViewID 分配网络视图ID

发布于 2019-12-18 15:38:06 字数 3299 浏览 1063 评论 0 收藏 0

JavaScript => public static function AllocateViewID(): NetworkViewID;
C# => public static NetworkViewID AllocateViewID();

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上着将变得更复杂。

JavaScript:

var cubePrefab : Transform;
 
var nView: NetworkView;
 
function Start() {
	nView = GetComponent.<NetworkView>();
}
 
function OnGUI() {
	if (GUILayout.Button("SpawnBox")) {
		var viewID = Network.AllocateViewID();
 
		nView.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;
}

C#:

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

network

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文