Version: 2021.3

Object.Instantiate

切换到手册
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 新对象的位置。
rotation 新对象的方向。
parent 将指定给新对象的父对象。
instantiateInWorldSpace When you assign a parent Object, pass true to position the new object directly in world space. Pass false to set the Object’s position relative to its new parent.

返回

Object 实例化的克隆对象。

描述

克隆 original 对象并返回克隆对象。

此函数会通过与编辑器中的复制命令类似的方式创建对象的副本。如果要克隆 GameObject,则可以指定其位置和旋转(否则,这些默认为原始 GameObject 的位置和旋转)。如果要克隆 Component,则也会克隆它附加到的 GameObject(同样可指定可选的位置和旋转)。

克隆 GameObjectComponent 时,也将克隆所有子对象和组件,它们的属性设置与原始对象相同。

Note: When this method clones a child object, it also clones the child's own children. To prevent stack overflow, Unity limits this nested cloning. If you exceed more than half your stack size, Unity throws an InsufficientExecutionStackException.

默认情况下,新对象的父对象 为 null;它与原始对象不"同级”"。但是,仍可以使用重载方法设置父对象。如果指定了父对象但未指定位置和旋转,则使用原始对象的位置和旋转作为克隆对象的本地位置和旋转或是其世界位置和旋转(如果 instantiateInWorldSpace 参数为 true)。如果指定了位置和旋转,则使用它们作为对象在世界空间中的位置和旋转。

克隆时 GameObject 的活动状态会维持,因此,如果原始对象处于非活动状态,则克隆对象也会在非活动状态下创建。此外,对于层级视图中的对象和所有子对象,其每个 Monobehaviour 和 Component 都会仅当调用此方法时它们在层级视图中处于活动状态时,才会调用其 Awake 和 OnEnable 方法。

这些方法不会创建与新实例化对象的预制件连接。可以使用 PrefabUtility.InstantiatePrefab 创建具有预制件连接的对象。

另请参阅:

在运行时实例化预制件
PrefabUtility.InstantiatePrefab

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

using UnityEngine;

public class Example : MonoBehaviour { public GameObject prefab; void Start() { for (var i = 0; i < 10; i++) { Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity); } } }

实例化可以用于在运行时创建新对象。示例包括:用于飞弹的对象或是用于爆炸特效的粒子系统。

using UnityEngine;

// Instantiate a rigidbody then set the velocity

public class Example : MonoBehaviour { // Assign a Rigidbody component in the inspector to instantiate

public 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 也可以直接克隆脚本实例。 将克隆整个游戏对象层级视图,并返回克隆的脚本实例。

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 = 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 的对象。

描述

You can also use Generics to instantiate objects. See the Generic Functions page for more details.

通过使用泛型,我们不需要将结果转换为特定类型。

using UnityEngine;

public class Missile : MonoBehaviour { // ...other code... }

public class InstantiateGenericsExample : MonoBehaviour { public Missile missile;

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