original | 要复制的现有对象。 |
position | 新对象的位置。 |
rotation | 新对象的方向。 |
parent | 将指定给新对象的父对象。 |
instantiateInWorldSpace | 分配父对象时,传递 true 可直接在世界空间中定位新对象。传递 false 可相对于其新父项来设置对象的位置。 |
Object 实例化的克隆对象。
克隆 original
对象并返回克隆对象。
此函数会通过与编辑器中的复制命令类似的方式创建对象的副本。如果要克隆 GameObject,则可以指定其位置和旋转(否则,这些默认为原始 GameObject 的位置和旋转)。如果要克隆 Component,则也会克隆它附加到的 GameObject(同样可指定可选的位置和旋转)。
克隆 GameObject 或 Component 时,也将克隆所有子对象和组件,它们的属性设置与原始对象相同。
默认情况下,新对象的父对象 为 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; 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); } } }
实例化可以用于在运行时创建新对象。示例包括:用于飞弹的对象或是用于爆炸特效的粒子系统。
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 设置附加到克隆对象的特定组件的属性。
original | 要克隆的类型为 T 的对象。 |
T 类型为 T 的对象。
您还可以使用泛型来实例化对象。有关更多详细信息,请参阅泛型函数页面。
通过使用泛型,我们不需要将结果转换为特定类型。
using UnityEngine;
public class Missile : MonoBehaviour { // ...other code... }
public class InstantiateGenericsExample : MonoBehaviour { public Missile missile;
void Start() { Missile missileCopy = Instantiate<Missile>(missile); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.