Unity3D基础教程4-1:在运行时初始化预制品

2014-08-14 19:47:13|?次阅读|上传:huigezrx【已有?条评论】发表评论

关键词:Unity3D, 游戏, 虚拟现实|来源:唯设编程网

Unity Manual > User Guide > Creating Gameplay > Instantiating Prefabs at runtime

Unity手册->用户指南->创建可玩性游戏->在运行时初始化预制品

By this point you should understand the concept of Prefabs at a fundamental level. They are a collection of predefined GameObjects & Components that are re-usable throughout your game. If you don't know what a Prefab is, we recommend you read the Prefabs page prior to this one.

在这之前你应该基本理解Prefabs的概念。他们是一个预定义的GameObjects及组件的集合,可在你的游戏重复使用。如果你不知道什么是预制品,我们建议你在这一页之前阅读Prefabs的基本概念。

Prefabs come in very handy when you want to instantiate complicated GameObjects at runtime. The alternative to instantiating Prefabs is to create GameObjects from scratch using code. Instantiating Prefabs has many advantages over the alternative approach:

Prefabs派上用场,当你要想在运行的时候完成GameObjects初始化。可供选择的初始化Prefabs是从头开始使用的代码创建GameObjects。初始化Prefabs有很多优点方法:

  • You can instantiate a Prefab from one line of code, with complete functionality. Creating equivalent GameObjects from code takes an average of five lines of code, but likely more.
  • 你可以用一行代码初始化,利用完成的功能。从代码可以创造同等GameObjects需要平均5行代码,但可能更多。
  • You can set up, test, and modify the Prefab quickly and easily in the Scene and Inspector.
  • 你可以在场景里及检视器里迅速并容易的设置,测试及修改预制品。
  • You can change the Prefab being instanced without changing the code that instantiates it. A simple rocket might be altered into a super-charged rocket, and no code changes are required.
  • 你可以修改预制品在初始化时不需要修改代码而初始化它。一个简单的火箭可能会改变成超级火箭,并没有代码需要改变。

Common Scenarios 常见方案

To illustrate the strength of Prefabs, let's consider some basic situations where they would come in handy:

为了说明预制品的强大,让我们考虑一些基本情况,它们将在哪里派上用场:

  1. Building a wall out of a single "brick" Prefab by creating it several times in different positions.

在不同的位置创建几个独立的“brick”(砖)预制品的墙

  1. A rocket launcher instantiates a flying rocket Prefab when fired. The Prefab contains a Mesh, Rigidbody, Collider, and a child GameObject with its own trail Particle System.

当火箭筒发射时,一个火箭筒实例飞行预制品。预制品包含一个网格,刚体、碰撞器以及一个拥有它自己的痕迹的粒子系统的子Gameobject。

  1. A robot exploding to many pieces. The complete, operational robot is destroyed and replaced with a wrecked robot Prefab. This Prefab would consist of the robot split into many parts, all set up with Rigidbodies and Particle Systems of their own. This technique allows you to blow up a robot into many pieces, with just one line of code, replacing one object with a Prefab.

一个机器人爆炸成多个分片。完整的业务机器人被摧毁和一个破坏机器人预制品所取代。这预制将由分裂的机器人组成许多部分,所有设置利用它们自己的Rigidbodies(刚体)和颗粒系统。这种技术允许你炸毁一个机器人成多块,仅有一行代码,利用一个预制品替代一个对象。

Building a wall 创建一个墙

This explanation will illustrate the advantages of using a Prefab vs creating objects from code.

这将说明使用一个预制品的优点与从代码创建对象的对比。

First, lets build a brick wall from code: 首先,让我们从代码创建一个砖头墙:

function Start () {

    for (var y = 0; y < 5; y++) {

        for (var x = 0; x < 5; x++) {

            var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);

            cube.AddComponent(Rigidbody);

            cube.transform.position = Vector3 (x, y, 0);

        }

    }

}
  • To use the above script we simply save the script and drag it onto an empty GameObject.
  • Create an empty GameObject with GameObject->Create Empty.
  • 使用上面的脚本我们单独保存这个脚本并拖拽它到一个空白的GameObject上。建立一个空白的脚本利用GameObject->Create Empty
发表评论0条 】
网友评论(共?条评论)..
Unity3D基础教程4-1:在运行时初始化预制品