Unity3D基础教程3-7:使用脚本(Using Scripts)

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

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

如果你是新的脚本,它看起来有些凌乱。这些是重要的概念理解:

  1. function Update () {} is a container for code that Unity executes multiple times per second (once per frame). function Update () {}是Unity多个时间每秒钟执行代码的容器(一次每帧)。
  2. transform is a reference to the GameObject's Transform Component. Transform是一个对象物体的变换组件的引用。
  3. Rotate() is a function contained in the Transform Component. Rotate()在变换组件里的功能容器。
  4. The numbers in-between the commas represent the degrees of rotation around each axis of 3D space: X, Y, and Z. 在逗号之间的数字表示环绕每个3D空间空间轴:x,y和z 的旋转的角度。
  5. Time.deltaTime is a member of the Time class that evens out movement over one second, so the cube will rotate at the same speed no matter how many frames per second your machine is rendering. Therefore, 5 * Time.deltaTime means 5 degrees per second.

Time.deltaTimeTime类的一个成员,使每秒移动平坦,因此这个Cube将以同样的速度旋转而不管你机器的每秒的多少帧被绘制。所以,5 * Time.deltaTime意味着每秒5度。

With all this in mind, we can read this code as "every frame, rotate this GameObject's Transform component a small amount so that it will equal five degrees around the Y axis each second."

所有在这一点,我们可以阅读这个代码作为“每一个帧,旋转这个GameObject的变换组件一个小的数量因此它将等于5度环绕Y轴每秒”

You can access lots of different Components the same way as we accessed transform already. You have to add Components to the GameObject using the Component menu. All the Components you can easily access are listed under Variables on the GameObject Scripting Reference Page.

同样的方法你可以访问许多不同组件是与我们已经访问了transform的一样方法。你必须添加组件到GameObject使用组件菜单。所有组件你可以容易访问被在GameObject脚本引用页的变量下列出。

For more information about the relationship between GameObjects, Scripts, and Components, please jump ahead to the GameObjects page or Using Components page of this manual.

关于GameObjects, Scripts和Components间的关系的更多信息,请跳到前面的GameObjects页或本手册的Using Components页。

The Power of Variables 变量的功能

Our script so far will always rotate the Cube 5 degrees each second. We might want it to rotate a different number of degrees per second. We could change the number and save, but then we have to wait for the script to be recompiled and we have to enter Play mode before we see the results. There is a much faster way to do it. We can experiment with the speed of rotation in real-time during Play mode, and it's easy to do.

我们的脚本到目前为止将总每秒选择Cube 5度。我们也许想要它每秒选择不同数量的角度。我们可以改变数据并保存它,但我们必须等待脚本被从新编译以及我们必须进入播放模式在我们看到结果之前。有一个非常快速的方法去做它。在实时播放模式过程中,我们可以利用旋转速度实验,而且它是容易去做到。

Instead of typing 5 into the Rotate() function, we will declare a speed variable and use that in the function. Change the script to the following code and save it:

替代在Rotate()函数输入5,我们将定义一个speed 变量并在函数里使用它。修改脚本到如下的代码并保存它。

var speed = 5.0;

function Update () {

    transform.Rotate(0, speed*Time.deltaTime, 0);

}

Now select the Cube and look at the Inspector. Notice how our speed variable appears.

现在,选取这个Cube并在检视器里查看。注意我们的speed变量如何显示。

Unity3D基础教程3-7:使用脚本(Using Scripts)

This variable can now be modified directly in the Inspector, just like renaming a file in the file explorer. Select it, press Return and change the value. You can also right- or option-click on the value and drag the mouse up or down. You can change the variable at any time, even while the game is running.

在检视器里的这个变量现在可以被直接修改,就像在文件资源管理器里重命名一个文件一样。选取它,按回车键并修改它的值。你也可以恰当的点击选项并左右拖动鼠标(修改值)。你可以在任何时间更改变量,甚至当游戏在运行的过程中。

Hit Play and try modifying the speed value. The Cube's rotation speed will change instantly. When you exit Play mode, you'll see that your changes are reverted back to their value before entering Play mode. This way you can play, adjust, and experiment to find the best value, then apply that value permanently.

击播放按钮并尝试修改speed变量的值。这个Cube的旋转速度将立即改变。等你退出播放模式时,你将看到你的更改回复到进入播放模式之前的它们值。这个方法你可以播放、调整以及实验去找打最优的值,然后应用不变的引用那个值。

Using this method of changing a variable's value in the Inspector means that you can re-use one script on many objects, each with a different variable value. If you attach the script to multiple Cubes, and change the speed of each cube, they will all rotate at different speeds even though they use the same script.

在检视器里使用这个方法修改变量的值意味着你可以重复使用一个脚本在多个对象上,每次用一个不同的变量值。如果你附加这个脚本到多个Cube,并更改每个Cube的speed,它们将全部用不同的速度旋转甚至是它们使用同样的脚本。

Where to go from here 从这里走到哪里

This was just a short introduction on how to use scripts inside the Editor. For more examples, check out the Tutorials that come with Unity. You should also read through Scripting Overview inside the Script Reference, which contains a more thorough introduction into scripting with Unity plus pointers into the reference itself for in-depth information. If you're really stuck, be sure to visit the Unity Forums and ask questions there. Someone is always willing to help.

这仅是一个短小的介绍如何使用脚本在编辑器里。更多的例子,检查Unity带来的手册。你也应该通过阅读脚本概述在脚本引用里,它包含了一个脚本到自己的参考指针与Unity加更详细的介绍深入的信息。如果你真的坚持,一定要访问Unity论坛并要求咨询,有人总是愿意提供帮助。

<123>
发表评论0条 】
网友评论(共?条评论)..
Unity3D基础教程3-7:使用脚本(Using Scripts)