Version: 2018.4
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コピーしたい既存オブジェクト
positionPosition for the new object.
rotationOrientation of the new object.
parentParent that will be assigned to the new object.
instantiateInWorldSpacePass 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 のオブジェクトをクローンします

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

See Also: In depth Prefab Instantiate discussion.

// 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); } } }

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

using UnityEngine;

public class Example : MonoBehaviour { // Instantiate a rigidbody then set the velocity

Rigidbody projectile;

void Update() { // Ctrl was pressed, launch a projectile if (Input.GetButtonDown("Fire1")) { // Instantiate the projectile at the position and rotation of this transform Rigidbody clone; 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); } } }

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 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; } } }

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


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 Missile : MonoBehaviour { // ...other code... }

public class InstantiateGenericsExample : MonoBehaviour { public Missile missile;

void Start() { Missile missileCopy = Instantiate<Missile>(missile); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961