Object.Instantiate 实例

static function Instantiate (original : Object, position : Vector3rotation : 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)命令是一样的,并移动到指定的位置。如果一个游戏物体,组件或脚本实例被传入,实例将克隆整个游戏物体层次,以及所有子对象也会被克隆。所有游戏物体被激活。

// 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敌人,粒子爆炸或破坏物体的替代品。

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

实例化也能直接克隆脚本实例,整个游戏物体层次将被克隆,并且返回被克隆脚本的实例

// 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 aComponent or a GameObject 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 或GameObject,整个游戏物体包含所有组件将被克隆,如果游戏物体有一个transform,所有子物体将被复制。所有游戏物体克隆之后被激活。

// 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(脚本)。

转载于:https://www.cnblogs.com/vincentDr/p/3679658.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值