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 parent を指定するときに、元のワールドの位置が維持されるかどうか

戻り値

Object The instantiated clone.

説明

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

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 the original GameObject's position and rotation otherwise). If you are cloning a Component then the GameObject it 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.

By default 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 using the overloaded methods. If a parent is specified and no position and rotation is specified, the position and rotation of the original will be used for the cloned object's local position and rotation, or its world position and rotation if the instantiateInWorldSpace parameter is true. If the position and rotation is specified, they will be used as the object's position and rotation in world space.

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.

For an in-depth tutorial of Instantiate and its uses, visit: In depth Prefab Instantiate discussion.

この例では、プレハブオブジェクトのクローンのインスタンスを、X軸に沿った線上に10個作成します。

using UnityEngine;

public class InstantiateExample : MonoBehaviour { public GameObject prefab;

void Start() { for (int i = 0; i < 10; i++) Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity); } }

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

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

using UnityEngine;

public class InstantiateComponentExample : MonoBehaviour { // Instantiate a prefab with an attached Missile script public Missile missilePrefab;

void Start() { // Instantiate the missile at the position and rotation of this object's transform Missile clone = (Missile)Instantiate(missilePrefab, transform.position, transform.rotation); } }

public static T Instantiate (T original);

パラメーター

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

戻り値

T T 型のオブジェクト

説明

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

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

using UnityEngine;

public class InstantiateGenericsExample : MonoBehaviour { public Missile missile;

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