返回介绍

Object.Instantiate 实例化

发布于 2019-12-18 15:38:09 字数 5621 浏览 1171 评论 0 收藏 0

JavaScript => static function Instantiate(original: Object, position: Vector3, rotation: Quaternion): Object;
JavaScript => static function Instantiate(original: Object): Object;

C# => static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
C# => static Object Instantiate(Object original);

Parameters

originalAn existing object that you want to make a copy of. 你想要拷贝的已有对象
positionPosition for the new object. 新对象的位置
rotationOrientation of the new object. 新对象的方向

Description

Clones the object original and returns the clone.

克隆原始物体并返回克隆物体。

This function makes a copy of an object in a similar way to the Duplicate command in the editor. If you are cloning a GameObject then you can also optionally specify its position and rotation (these will default to Vector3.zero and Quaternion.identity respectively). If you are cloning a Component then the GameObject is is attached to will also be cloned, again with an optional position and rotation. When you clone a GameObject or Component, all child objects and components will also be cloned with their properties set like those of the original object. However, the parent of the new object will be null, so it will not be a “sibling” of the original. However, you can still set the parent explicitly if you wish. Also, the active status of a GameObject at the time of cloning will be passed on, so if the original is inactive then the clone will be created in an inactive state too.

克隆原始物体,位置设置在position,设置旋转在rotation,返回的是克隆后的物体。这实际上在Unity和使用复制(ctrl+D)命令是一样的,并移动到指定的位置。如果一个游戏物体,组件或脚本实例被传入,实例将克隆整个游戏物体层次,以及所有子对象也会被克隆。所有游戏物体被激活。

See Also: In depth Prefab Instantiate discussion.

JavaScript:

	// Instantiates 10 copies of prefab each 2 units apart from each other
 
	var prefab : Transform;
 
	for (var i : int = 0;i < 10; i++) {
		Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity);
	}

C#:

using UnityEngine;
using System.Collections;
 
public class Example : MonoBehaviour {
    public Transform prefab;
    void Example() {
        int i = 0;
        while (i < 10) {
            Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity) as Transform;
            i++;
        }
    }
}

Instantiate is most commonly used to instantiate projectiles, AI Enemies, particle explosions or wrecked object replacements.

实例化更多通常用于实例投射物(如子弹、榴弹、破片、飞行的铁球等),AI敌人,粒子爆炸或破坏物体的替代品。

JavaScript:

// Instantiate a rigidbody then set the velocity
 
var projectile : Rigidbody;
 
function Update () {
	// Ctrl was pressed, launch a projectile
	if (Input.GetButtonDown("Fire1")) {
		// Instantiate the projectile at the position and rotation of this transform
		var clone : Rigidbody;
		clone = Instantiate(projectile, transform.position, transform.rotation);
 
		// Give the cloned object an initial velocity along the current 
		// object's Z axis
		clone.velocity = transform.TransformDirection (Vector3.forward * 10);
	}
}

C#:

using UnityEngine;
using System.Collections;
 
public class Example : MonoBehaviour {
    public Rigidbody projectile;
    void Update() {
        if (Input.GetButtonDown("Fire1")) {
            Rigidbody clone;
            clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
            clone.velocity = transform.TransformDirection(Vector3.forward * 10);
        }
    }
}

Instantiate can also clone script instances directly. The entire game object hierarchy will be cloned and the cloned script instance will be returned.

实例化也能直接克隆脚本实例,整个游戏物体层次将被克隆,并且返回被克隆脚本的实例

JavaScript:

	// Instantiate a prefab with an attached Missile script
	var projectile : Missile;
 
	function Update () {
		// Ctrl was pressed, launch a projectile
		if (Input.GetButtonDown("Fire1")) {
			// Instantiate the projectile at the position and rotation of this transform
			var clone : Missile;
			clone = Instantiate(projectile, transform.position, transform.rotation);
 
			// Set the missiles timeout destructor to 5
			clone.timeoutDestructor = 5;
		}
	}

C#:

using UnityEngine;
using System.Collections;
 
public class DestroyTest : MonoBehaviour {
 
	Missile projectile;
 
	void Update () {
 
		if (Input.GetButtonDown("Fire1")) {
 
			Missile clone;
			clone = Instantiate(projectile, transform.position, transform.rotation) as Missile;
 
			clone.timeoutDestructor = 5;
		}
	}
}

After cloning an object you can also use GetComponent to set properties on a specific component attached to the cloned object.

在克隆物体之后,你也可以使用GetComponent设置附加到克隆的物体特定组件的属性。

Description

Generic version. See the Generic Functions page for more details.

泛型版本。更多细节参见泛型函数页面。

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

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

发布评论

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