📜 ⬆️ ⬇️

New in Unity 2017.3. Compiling scripts into separate build files

In previous versions of Unity, the scripts were assembled into a single Assembly-CSharp.dll.
And when adding new scripts, the compile time was increased. Now in the new version it is possible to define your own managed assemblies based on scripts inside a folder, which significantly reduces compilation time, especially in large projects. Now you can think of each managed assembly as a single library in a Unity project.

image

The figure above shows how you can split project scripts into multiple assemblies. If you only modify the scripts in Main.dll, none of the other builds need to be recompiled. Since Main.dll contains fewer scripts, it also compiles faster than Assembly-CSharp.dll.

Similarly, changes to the scripts you make in Stuff.dll will only lead to recompilation of the Main.dll and Stuff.dll files.
')

How to use build definition files


Assembly definition files are asset files that you create by going to Assets → Create → Assembly Definition. This is a file with the .asmdef extension.

Note. The name of the folder containing the assembly definition file and the file name of the assembly definition file do not affect the assembly name.

image

Example:

Create a folder Scripts. And her folder Locomotion, Editor, Utils. There are two more folders in the Locomotion folder, Normal and Anvanced. Now create scripts

Utils → MoveExtension.cs

Locomotion → Anvanced → AdvJump.cs
Locomotion → Anvanced → AdvMoveForward.cs

Locomotion → Normal → Jump.cs MoveForward
Locomotion → Normal → MoveForward.cs

Locomotion → LookForward.cs

Editor → MoveForwardEditor.cs

And we will define a bit of code in the MoveForwardEditor script so that we need a reference to MoveForward.

using UnityEditor; [CustomEditor(typeof(MoveForward))] public class MoveForwardEditor : Editor { } 

Now in order for us to create managed assemblies, we need to create .asmdef files in the scripts folder. Moreover, if this file is in a folder, locomotion is allowed, then the scripts from the Advanced and Normal folders will also be picked up in the assembly file, if these folders do not contain their own assembly files.

In the Utils folder we create the UtilityAsm.asmdef assembly
In the Locomotion folder, create the LocomotionAsm.asmdef assembly file.
In the folder Locomotion → Anvanced → LocomotionAdvancedAsm.asmdef
In the Editor EditorAsm.asmdef folder

In the folder Locomotion → Normal do not create anything, because scripts from this folder will get into the LocomotionAsm assembly

And here we get the error ...

Assets / Scripts / Editor / MoveForwardEditor.cs (3.22): error CS0246: The type or namespace name `MoveForward 'could not be found. Are you missing an assembly reference?

image

Script MoveForwardEditor swears that it can not find a link to MoveForward. Everything is correct in this case, MoveForward was in a different assembly and is not available. To fix this you need to do the following

1. Select the EditorAsm assembly file
2. Click on +
3. Select the assembly file LocomotionAsm, because MoveForward is there.
4. From the platforms leave only Editor
5. click Apply

image

Now the error is gone. And let's look at the Library folder and see our assemblies.

image

After building the project, we will also see these assemblies, except EditorAsm, since This is the editor's script.

There is another important note. Assembly files do not support preprocessor directives, since always static.

The article was written as an attempt to translate the official blog ) Thank you all for reading the article. Waiting for your comments.

Source: https://habr.com/ru/post/347034/


All Articles