查看: 2352|回复: 0
打印 上一主题 下一主题

[脚本章节]Overview: Instantiate 实例

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2012-10-11 10:25:54 |只看该作者 |倒序浏览
Instantiate duplicates an object. Including all attached scripts and the entire hierarchy. It keeps references in the way you expect it: References to objects outside the cloned hierarchy will be left untouched, References to objects in the cloned hierarchy will map to the cloned object.
实例化一个对象,将包含该对象已有的全部脚本和它的全部继承关系(比如子物体和组件).这可以保证你以期望的方式引用它:可以原封不动的将对象的继承关系复制到你的新对象中.
Instantiate is incredibly fast and very versatile. It is essential to using Unity to its fullest.
实例化非常快捷,而且是多用途的.在使用Unity的过程中充分利用它是非常必要的.
For example here is a tiny script that when attached to a rigidbody with collider will destroy itself and instead spawn an explosion object
例如这是一个小脚本,当碰撞体接触后销毁自身对象,并创建一个爆炸对象.


    C#
    JavaScript


using UnityEngine;using System.Collections;public class example : MonoBehaviour {    public Transform explosion;    void OnCollisionEnter() {        Destroy(gameObject);        Transform theClonedExplosion;        theClonedExplosion = Instantiate(explosion, transform.position, transform.rotation);    }}


var explosion : Transform ;// When a collision happens destroy ourselves// and spawn an explosion prefab instead// 当一个碰撞发生自毁// 和产生爆炸预制物代替function OnCollisionEnter (){    Destroy (gameObject);    var theClonedExplosion : Transform ;    theClonedExplosion = Instantiate(explosion,transform.position, transform.rotation);}


Instantiate is usually used in conjunction with Prefabs .
实例化常和 Prefabs 一起使用.



来源:unity3d圣典 更多分享尽在web3D纳金网http://www.narkii.com/



----------------------------------------------------------------------------------


Subsections 章节

     [脚本章节]Scripting Overview 脚本概述
    [脚本章节]Overview: Script compilation 脚本编译(高级)
    [脚本章节]Overview: Performance Optimization 性能优化
    [脚本章节]Overview: The most important classes 重要的类
    [脚本章节]Overview : Writing Scripts in C# 使用C#书写脚本
    [脚本章节]Overview: Coroutines & Yield 协同程序 & 中断
    [脚本章节]Overview: Instantiate 实例
    [脚本章节]Overview: Member Variables & Global Variables 成员变量 & 全局变量
    [脚本章节]Overview: Vectors 向量
    [脚本章节]Overview: Accessing Other Game Objects 访问其他游戏物体
    [脚本章节]Overview: Accessing Other Components 访问其他组件
    [脚本章节]Overview: Keeping Track of Time 记录时间
    [脚本章节]Overview: Common Operations 常用操作
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-4-24 02:46 , Processed in 0.099908 second(s), 33 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部