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)命令是一樣的,並移動到指定的位置。如果一個遊戲物體,組件或腳本實例被傳入,實例將克隆整個遊戲物體層次,以及所有子對象也會被克隆。所有遊戲物體被激活。

// Instantiates 10 copies of prefab each 2 units apart from each other
//實例化10個 prefab拷貝,間隔2個單位
var prefab : Transform;

for (var i : int = 0;i < 10; i++) {
	Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity);
}

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

實例化更多通常用於實例投射物(如子彈、榴彈、破片、飛行的鐵球等),AI敵人,粒子爆炸或破壞物體的替代品。

// Instantiate a rigidbody then set the velocity
//實例化一個剛體,然後設置速度
var projectile : Rigidbody;

function Update () {
	// Ctrl was pressed, launch a projectile
	//按Ctrl發射炮彈
	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
		//沿着當前物體的Z軸給克隆的物體一個初速度。
		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.

實例化也能直接克隆腳本實例,整個遊戲物體層次將被克隆,並且返回被克隆腳本的實例

// Instantiate a prefab with an attached Missile script
//實例化一個附加在Missile腳本的預設
var projectile : Missile;

function Update () {
	// Ctrl was pressed, launch a projectile
	//按Ctrl發射炮彈
	if (Input.GetButtonDown("Fire1")) {
		// Instantiate the projectile at the position and rotation of this transform
		//在該變換位置和旋轉,實例化projectile
		var clone : Missile;
		clone = Instantiate(projectile, transform.position, transform.rotation);

		// Set the missiles timeout destructor to 5
		//設置missiles超時5秒爆炸
		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 a Component or aGameObject 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,所有子物體將被複制。所有遊戲物體克隆之後被激活。

// Instantiates prefab when any rigid body enters the trigger.
// It preserves the prefab's original position and rotation.
//當任何剛體進入觸發器時實例化prefab
//它保留prefab的原始位置和旋轉
var prefab : Transform;

function OnTriggerEnter () {
	Instantiate (prefab);
}

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

注意,Instantiate(實例化)能克隆Object(物體)任何類型,包含script(腳本)。

發佈了50 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章