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

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

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

第一人称射击指南讲解如何去替代一个角色利用一个布娃娃版本和同步四肢最后状态的动画。你以在指南页发现那个指南。

Placing a bunch of objects in a specific pattern把一束对象放一个特定的图案中

Lets say you want to place a bunch of objects in a grid or circle pattern. Traditionally this would be done by either:

对你来说想要把一束物件放一个方格里或圆周图案中。传统地会采用下列之一做:

  1. Building an object completely from code. This is tedious! Entering values from a script is both slow, unintuitive and not worth the hassle.   完全的从代码创建一个对象。这是乏味的!从一个脚本输入值是两倍的慢,直觉的不值得麻烦。
  2. Make the fully rigged object, duplicate it and place it multiple times in the scene. This is tedious, and placing objects accurately in a grid is hard.

在场景里,制造完全装配的对象,复制它并放置它多次。这是沉闷的,而且正确放置对象在网格里是困难的。

So use Instantiate() with a Prefab instead! We think you get the idea of why Prefabs are so useful in these scenarios. Here's the code necessary for these scenarios:

因此使用Instantiate()一个预制品替代。我们认为你获得一个想法:在这些场景里为什么预制品是如此有用。这些场景所需的代码在这里:

// Instantiates a prefab in a circle

var prefab : GameObject;

var numberOfObjects = 20;

var radius = 5;

function Start () {

    for (var i = 0; i < numberOfObjects; i++) {

        var angle = i * Mathf.PI * 2 / numberOfObjects;

        var pos = Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;

        Instantiate(prefab, pos, Quaternion.identity);

    }

}

// Instantiates a prefab in a grid

var prefab : GameObject;

var gridX = 5;

var gridY = 5;

var spacing = 2.0;

function Start () {

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

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

            var pos = Vector3 (x, 0, y) * spacing;

            Instantiate(prefab, pos, Quaternion.identity);

        }

    }

}
发表评论0条 】
网友评论(共?条评论)..
Unity3D基础教程4-1:在运行时初始化预制品