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

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

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

When executed, this code will print "Hello World" to the console. But there is nothing that causes the code to be executed yet. We have to attach the script to an active GameObject in the Scene before it will be executed.

当执行,这个代码将打印"Hello World"到控制台里。但什么也没有导致代码的执行。在它执行之前,在场景里,我们必须附加脚本到一个活动的GameObject。

Attaching scripts to objects 附加脚本到对象

Save the above script and create a new object in the Scene by selecting GameObject->Create Other->Cube. This will create a new GameObject called "Cube" in the current Scene.

保存上面的脚本并建立一个新的对象在场景里通过选取GameObject->Create Other->Cube这将建立一个新的游戏物体称为"Cube”在当前场景里。

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

Now drag the script from the Project View to the Cube (in the Scene or Hierarchy View, it doesn't matter). You can also select the Cube and choose Component->Scripts->New Behaviour Script. Either of these methods will attach the script to the Cube. Every script you create will appear in the Component->Scripts menu. If you have changed the name of your script, you will that name instead.

现在从项目视图里拖拽脚本到这个立方体(Cube)(在场景里或层次视图里,这无关紧要)。你也可以选取这这个Cube并选择Component->Scripts->New Behaviour Script这些方法的编辑器将附加这个脚本到Cube。你建立的每个脚本将显示在Component->Scripts菜单里。如果你已经修改了你的脚本的名称,你将代替那个名称。

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

If you select the Cube and look at the Inspector, you will see that the script is now visible. This means it has been attached.


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

Press Play to test your creation. You should see the text "Hello World" appear beside the Play/Pause/Step buttons. Exit play mode when you see it.

按播放按钮去测试你的创造。你应该看到"Hello World"文本显示Play/Pause/Step按钮旁边 (在2.5的windows 版的里,显示在底部状态栏)。当你看到它退出播放模式。

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

Manipulating the object 操作对象

A print() statement can be very handy when debugging your script, but it does not manipulate the GameObject it is attached to. Let's change the script to add some functionality:


function Update () {

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


If you're new to scripting, it's okay if this looks confusing. These are the important concepts to understand:

发表评论0条 】
Unity3D基础教程3-7:使用脚本(Using Scripts)