返回介绍

Network.Instantiate 实例化

发布于 2019-12-18 15:38:07 字数 3338 浏览 1024 评论 0 收藏 0

JavaScript => public static function Instantiate(prefab: Object, position: Vector3, rotation: Quaternion, group: int): Object;
C# => public static Object Instantiate(Object prefab, Vector3 position, Quaternion rotation, int group);

Parameters 参数

Description 描述

Network instantiate a prefab.

网络实例化预设。

The given prefab will be instanted on all clients in the game. Synchronization is automatically set up so there is no extra work involved. The position, rotation and network group number are given as parameters. Note that in the example below there must be something set to the playerPrefab in the Editor. You can read more about instantiations in the object reference Object.Instantiate.

给定的预设将在所有的客户端上实例化。同步被自动设置,因此没有额外的工作要做。位置、旋转和网络组数值作为给定的参数。这是一个RPC调用,因此,当为该组数Network.RemoveRPCs被调用,这个物体将被移除。注意在编辑器中必须设置playerPrefab,在Object.Instantiate物体参考中获取更多实例化信息。

Internally this is a buffered RPC call. You can use Network.RemoveRPCs is with the group number to remove the call from the buffer. Alternatively you can call Network.RemoveRPCs on the first NetworkViewID in the instantiated prefab. When the instantiation occurs internally the buffered RPC message is linked to the first NetworkView of the prefab which makes this possible.

JavaScript:

// Immediately instantiate new connected player's character
	// when successfully connected to the server.
 
	var playerPrefab : Transform;
	function OnConnectedToServer () {
		Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
	}

JavaScript:
Example of removing an instantiation from the RPC buffer:

var nView: NetworkView;
 
function Start() {
	nView = GetComponent.<NetworkView>();
}
 
// Immediately destroy and remove RPC for network instantiated object
// (script attached to prefab)
function OnNetworkInstantiate(info: NetworkMessageInfo) {
	Debug.Log(nView.viewID + " spawned");
 
	if (Network.isServer) {
		Network.RemoveRPCs(nView.viewID);
		Network.Destroy(gameObject);
	}
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public Transform playerPrefab;
    void OnConnectedToServer() {
        Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
    }
}

C#:
Example of removing an instantiation from the RPC buffer:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public NetworkView nView;
    void Start() {
        nView = GetComponent<NetworkView>();
    }
    void OnNetworkInstantiate(NetworkMessageInfo info) {
        Debug.Log(nView.viewID + " spawned");
        if (Network.isServer) {
            Network.RemoveRPCs(nView.viewID);
            Network.Destroy(gameObject);
        }
    }
}

network

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

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

发布评论

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