Instantiate 实例

Object.Instantiate 实例

static function Instantiate (original : Object, position : Vector3rotation : Quaternion) : Object

Description描述

Clones the object original and returns the clone.

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

Clones the object original, places it at position and sets the rotation to rotation, then returns the cloned object. This is essentially the same as using duplicate command (cmd-d) in Unity and then moving the object to the given location. If a game object, component or script instance is passed, Instantiate will clone the entire game object hierarchy, with all children cloned as well. All game objects are activated.

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

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Transform prefab;
	public void Awake() {
		int i = 0;
		while (i < 10) {
			Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
			i++;
		}
	}
}

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

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

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);
			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.

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

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Missile projectile;
	void Update() {
		if (Input.GetButtonDown("Fire1")) {
			Missile clone;
			clone = Instantiate(projectile, transform.position, transform.rotation);
			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设置附加到克隆的物体特定组件的属性。

• static function Instantiate (original : Object) : Object

Description描述

Clones the object original and returns the clone.

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

This function preserves the position and rotation of the cloned object. This is essentially the same as using duplicate command (cmd-d) in Unity. If the object is aComponent or a GameObject then entire game object including all components will be cloned. If the game object has a transform all children are cloned as well. All game objects are activated after cloning them.

这个函数保留被克隆物体的位置和旋转。这实际上等同于在Unity使用(duplicate)复制命令,如果物体是一个Component 或GameObject,整个游戏物体包含所有组件将被克隆,如果游戏物体有一个transform,所有子物体将被复制。所有游戏物体克隆之后被激活。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Transform prefab;
	void OnTriggerEnter() {
		Instantiate(prefab);
	}
}

Note that the Instantiate can clone any type of Object including scripts.

注意,Instantiate(实例化)能克隆Object(物体)任何类型,包含script(脚本)。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值