Overview: Script compilation (Advanced)

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

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.

Script compilation is performed in 4 steps:

1. All scripts in "Standard Assets", "Pro Standard Assets" or "Plugins" are compiled first.

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.

2. All scripts in "Standard Assets/Editor", "Pro Standard Assets/Editor" or "Plugins/Editor" are compiled next.

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.

These scripts can access scripts from the previous group.

3. All other scripts outside "Editor" are compiled next.

All scripts that are not in the folders above or in "Editor" are compiled next.

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.

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, put scripts that seldom change into group 1 and scripts that change a lot into group 3.

4. All scripts in "Editor" are compiled last.

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.

These scripts can access all scripts in all previous groups.

Additionally, any scripts in a folder called WebPlayerTemplates will not be compiled at all.

Conditional compilation against the Unity version.

Starting with Unity version 2.6 a C# preprocessor define has been added. The define identifies the version of Unity in use and is intended to allow conditional access to specific features. For example:


// Specific version define including the minor revision
#if UNITY_2_6_0
// Use Unity 2.6.0 specific feature
#endif

// Specific version define not including the minor revision
#if UNITY_2_6
// Use Unity 2.6.x specific feature
#endif

This code can be used to enable game features that are only available with a specific version of Unity. Note that this define is present from version 2.6 only. Future Unity versions will provide the appropriate define to identify the version to your scripts.

Note: for more information about conditional compilation for platform dependent defines, refer to the Unity Manual - Platform Dependent Compilation.