Unity3D圣典3.3版本-中文版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Object.Instantiate 实例
# [Object](Object.html "Go to Object scripting documentation").Instantiate 实例 static function *Instantiate* (*original* : Object,*position* : [Vector3](../Vector3/Vector3.html),*rotation* : Quaternion) : Object *Description* 描述 Clones the object original and returns the clone. 克隆原始物体并返回克隆物体。 Clones the object original, places it at position and sets the rotation to rotation, then returns the cloned object. This is essentially the same as using duplicate command (cmd-d) in Unity and then moving the object to the given location. If a game object, component or script instance is passed, Instantiate will clone the entire game object hierarchy, with all children cloned as well. All game objects are activated. 克隆原始物体,位置设置在 position,设置旋转在 rotation,返回的是克隆后的物体。这实际上在 Unity 和使用复制(ctrl+D)命令是一样的,并移动到指定的位置。如果一个游戏物体,组件或脚本实例被传入,实例将克隆整个游戏物体层次,以及所有子对象也会被克隆。所有游戏物体被激活。 * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { public Transform prefab; public void Awake() { int i = 0; while (i < 10) { Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity); i++; } } } ``` ``` // Instantiates 10 copies of prefab each 2 units apart from each other //实例化10个 prefab拷贝,间隔2个单位 var prefab : Transform; for (var i : int = 0;i < 10; i++) { Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity); } ``` Instantiate is most commonly used to instantiate projectiles, AI Enemies, particle explosions or wrecked object replacements. 实例化更多通常用于实例投射物(如子弹、榴弹、破片、飞行的铁球等),AI 敌人,粒子爆炸或破坏物体的替代品。 * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { public Rigidbody projectile; void Update() { if (Input.GetButtonDown("Fire1")) { Rigidbody clone; clone = Instantiate(projectile, transform.position, transform.rotation); clone.velocity = transform.TransformDirection(Vector3.forward * 10); } } } ``` ``` // Instantiate a rigidbody then set the velocity //实例化一个刚体,然后设置速度 var projectile : Rigidbody; function Update () { // Ctrl was pressed, launch a projectile //按Ctrl发射炮弹 if (Input.GetButtonDown("Fire1")) { // Instantiate the projectile at the position and rotation of this transform //在该变换位置和旋转,实例化炮弹 var clone : Rigidbody; clone = Instantiate(projectile, transform.position, transform.rotation); // Give the cloned object an initial velocity along the current object's Z axis //沿着当前物体的Z轴给克隆的物体一个初速度。 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. 实例化也能直接克隆脚本实例,整个游戏物体层次将被克隆,并且返回被克隆脚本的实例 * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { public Missile projectile; void Update() { if (Input.GetButtonDown("Fire1")) { Missile clone; clone = Instantiate(projectile, transform.position, transform.rotation); clone.timeoutDestructor = 5; } } } ``` ``` // Instantiate a prefab with an attached Missile script //实例化一个附加在Missile脚本的预设 var projectile : Missile; function Update () { // Ctrl was pressed, launch a projectile //按Ctrl发射炮弹 if (Input.GetButtonDown("Fire1")) { // Instantiate the projectile at the position and rotation of this transform //在该变换位置和旋转,实例化projectile var clone : Missile; clone = Instantiate(projectile, transform.position, transform.rotation); // Set the missiles timeout destructor to 5 //设置missiles超时5秒爆炸 clone.timeoutDestructor = 5; } } ``` After cloning an object you can also use GetComponent to set properties on a specific component attached to the cloned object. 在克隆物体之后,你也可以使用 GetComponent 设置附加到克隆的物体特定组件的属性。 • static function *Instantiate* (*original* : Object) : Object *Description* 描述 Clones the object original and returns the clone. 克隆原始物体并返回克隆物体。 This function preserves the position and rotation of the cloned object. This is essentially the same as using duplicate command (cmd-d) in Unity. If the object is a [Component](../Component/Component.html) or a [GameObject](../GameObject/GameObject.html) then entire game object including all components will be cloned. If the game object has a transform all children are cloned as well. All game objects are activated after cloning them. 这个函数保留被克隆物体的位置和旋转。这实际上等同于在 Unity 使用(duplicate)复制命令,如果物体是一个 [Component](../Component/Component.html) 或 [GameObject](../GameObject/GameObject.html),整个游戏物体包含所有组件将被克隆,如果游戏物体有一个 transform,所有子物体将被复制。所有游戏物体克隆之后被激活。 * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { public Transform prefab; void OnTriggerEnter() { Instantiate(prefab); } } ``` ``` // Instantiates prefab when any rigid body enters the trigger. // It preserves the prefab's original position and rotation. //当任何刚体进入触发器时实例化prefab //它保留prefab的原始位置和旋转 var prefab : Transform; function OnTriggerEnter () { Instantiate (prefab); } ``` Note that the Instantiate can clone any type of Object including scripts. 注意,Instantiate(实例化)能克隆 Object(物体)任何类型,包含 script(脚本)。
da
2022年5月23日 14:13
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码