Overview: Script compilation 脚本编译(高级)

Unity compiles all scripts to .NET dll files. The .dll files will be jit compiled at runtime.

Unity把脚本编译为.NET dll(动态链接库)文件. dll文件将在运行时编译执行.

This allows incredibly fast script execution. It is around 20 times faster than traditional javascript and around 50% slower than native C++ code. Unity might take a second to compile all your scripts when you save it. You can see if Unity is still compiling with the small spinning progress icon in the lower right corner of Unity's main window.

这使脚本以惊人的速度执行.这比传统的javascript快20倍,但比传统C++慢一半左右.在你保存的时候,Unity将花费一些时间编译你的脚本.如果Unity在编译,你能看到在主窗口角落有个活动的进度标示.

Script compilation is performed in 4 steps:

脚本编译包含4步:

1. All scripts in "Standard Assets", "Pro Standard Assets" or "Plugins" are compiled first.
所有"Standard Assets", "Pro Standard Assets"或"Plugins"文件夹里的脚本会被首先编译

Scripts in one of these folders can't directly access scripts outside these folders.

这些文件夹里的脚本不能直接访问文件夹之外的脚本.

It is not possible to reference the class or its variables directly, but it is possible to communicate with them using GameObject.SendMessage .

不能直接引用类或它们的变量,但是可以通过使用 GameObject.SendMessage 与他们进行通信.

2. All scripts in "Standard Assets/Editor", "Pro Standard Assets/Editor" or "Plugins/Editor" are compiled first.
所有"Standard Assets/Editor", "Pro Standard Assets/Editor"或"Plugins/Editor"文件夹里的脚本会被首先编译

If you want to use the UnityEditor namespace you have to place your scripts in these folders. For example to add menu items or write custom wizards you have to place your scripts in those folders.

如果你想使用UnityEditor命名空间,你必须把你的脚本放到这些文件夹里.例如你想添加菜单项或自定义向导,那么你必须把你的脚本放到那些文件夹里.

These scripts can access scripts from the previous group.

这些脚本可以访问之前第一条里说的那些脚本.

3. All scripts in "Editor" are compiled next.
所有在"Editor"里的脚本将紧接着被编译

If you want to use the UnityEditor namespace you have to place your scripts in these folders. For example to add menu items or write custom wizards you have to place your scripts in those folders.

如果你想使用UnityEditor命名空间,你必须把你的脚本放到这些文件夹里.例如你想添加菜单项或自定义向导,那么你必须把你的脚本放到那些文件夹里.

These scripts can access all scripts in all previous groups. However they can not access scripts, in the last group.

这些脚本可以访问之前提到的脚本组中的脚本.但是不能访问之后的脚本组中的脚本.

This can be a problem when wirting editor code since you often want to edit scripts that are in the last group. There are two solutions: 1. Move the other script into the "Plugins" folder. 2. Take advantage of JavaScript's dynamic typing. In Javascript you don't need to know the type of a class to work with it. When using GetComponent you can just use a string instead of the type. You can also use SendMessage, which takes a string.

这回导致一个问题,当你的编辑器代码要编辑其后面组内的脚本时该怎么办呢?有2种解决方法:1.把那些脚本放置到"Plugins"文件夹.2.利用JavaScript动态类型.在JavaScript你不需要知道类的类型,你可以使用GetComponent,因为它的参数可以只是个字符串而不需要指定类型.你也能使用SendMessage,它也可以只使用一个字符串.

4. All other scripts are compiled last.
其他所有脚本将在最后编译.

All scripts that are not in the folders above are compiled last.

所有不在上面说明的脚本都将在最后编译.

All scripts that are compiled in this step have access to all scripts in the first group ("Standard Assets", "Pro Standard Assets" or "Plugins"). This allows you to let different scripting languages interoperate. For example, if you want to create a Javascript that uses a C# script: place the C# script in the "Standard Assets" folder and the Javascript outside of the "Standard Assets" folder. The Javascript can now reference the C# script directly.

所有这一步里编译的脚本,可以访问第一组提到的所有脚本("Standard Assets", "Pro Standard Assets" or "Plugins").这允许你使用不同的脚本语言进行沟通.例如,你想用一个Javascript脚本引用一个C#脚本:放C#脚本到"Standard Assets"文件夹然后把Javascript脚本放在此文件夹之外. Javascript脚本便可以直接引用C#脚本.

Scripts that are placed in the first group, will take longer to compile, since when they are compiled the third group needs to be recompiled too. Thus if you want to reduce compile times, move scripts that seldomly change into group 1 and scripts that change a lot into group 4.

放在第一组的脚本,将需要更长的编译时间,因为你编译第三组时还需要再编译它.因此你想要减少编译时间,把不常变更的脚本放在第一组,常变的放到第四组.

最后修改:2010年11月30日 Tuesday 17:51

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。