public static Object Instantiate (Object original);
public static Object Instantiate (Object original, Transform parent);
public static Object Instantiate (Object original, Transform parent, bool instantiateInWorldSpace);
public static Object Instantiate (Object original, Vector3 position, Quaternion rotation);
public static Object Instantiate (Object original, Vector3 position, Quaternion rotation, Transform parent);

パラメーター

original コピーしたい既存オブジェクト
position Position for the new object.
rotation Orientation of the new object.
parent Parent that will be assigned to the new object.
instantiateInWorldSpace Pass true when assigning a parent Object to maintain the world position of the Object, instead of setting its position relative to the new parent. Pass false to set the Object's position relative to its new parent.

戻り値

Object The instantiated clone.

説明

original のオブジェクトをクローンします

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

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

オブジェクトをクローンした後、さらに GetComponent を使用して、クローンされたオブジェクトの特定コンポーネントのプロパティーを設定できます。

// Instantiates 10 copies of prefab each 2 units apart from each other

using UnityEngine; using System.Collections;

public class ExampleClass : MonoBehaviour { public Transform prefab; void Start() { for (int i = 0; i < 10; i++) { Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity); } } }
no example available in C#
using UnityEngine;
using System.Collections;

public class Missile : MonoBehaviour { public int timeoutDestructor;

// ...other code... }

public class ExampleClass : MonoBehaviour { // Instantiate a prefab with an attached Missile script public Missile projectile;

void Update() { // Ctrl was pressed, launch a projectile if (Input.GetButtonDown("Fire1")) { // Instantiate the projectile at the position and rotation of this transform Missile clone = (Missile)Instantiate(projectile, transform.position, transform.rotation);

// Set the missiles timeout destructor to 5 clone.timeoutDestructor = 5; } } }

public static T Instantiate (T original);
public static T Instantiate (T original, Transform parent);
public static T Instantiate (T original, Transform parent, bool worldPositionStays);
public static T Instantiate (T original, Vector3 position, Quaternion rotation);
public static T Instantiate (T original, Vector3 position, Quaternion rotation, Transform parent);

パラメーター

original クローンしたい T 型のオブジェクト

戻り値

T T 型のオブジェクト

説明

ジェネリック版も使用できます。詳細については Generic Functions を参照してください。

この例では Missile オブジェクトのインスタンスを再び作成していますが、Generics を使用しているので結果をキャストする必要がありません。

using UnityEngine;

public class InstantiateGenericsExample : MonoBehaviour { public Missile missile;

void Start() { Missile missileCopy = Instantiate<Missile>(missile); } }