What is [unity] assembly definition?

What is assembly definition

We know that unity automatically defines how scripts are compiled into managed assemblies. After all scripts are compiled, a managed assembly is generated. Naturally, as we add more and more scripts, the compilation time will be longer and longer. If the project becomes large enough, the waiting compilation time is quite terrible

In this way, the assembly definition file (assembly definition file) arises at the historic moment, which can be used to reduce the compilation time

principle basis:

The script is saved in different folders. An assembly definition file is generated from a folder containing multiple scripts. When one of the scripts is modified, only the assembly definition file containing the script will be recompiled, and the others will not be recompiled, so as to reduce the compilation time

As shown in the figure:

How to use assembly definition

Use the following menu to create an assembly definition file:

The created file is also regarded as a resource file. The suffix of the file ends with. Asmdef

How to set assembly definition

Create an assembly definition file in a folder. All scripts in the folder will be compiled into the assembly definition file. After selecting the assembly definition file

The inspector property panel is shown as follows:

First name: set the assembly name( The name of the folder where the assembly definition file is located, the name of the assembly definition file itself, and the assembly name set in the property panel do not affect each other.)

Second references: click the plus sign to add other assembly definition files referenced by the assembly definition file( Use references to compile assemblies in unity and define their direct dependencies)

Third, unity References: check this option if the assembly is used for testing( The assembly will not be added to the predefined assembly file, such as assembly-csharp.dll, nor will it be added to the packaging file.)

Fourth option: check this option if the unsafe keyword is used in the script in the assembly file

Fifth, platforms: set the platform compatibility of the assembly definition file( Some special platforms can be excluded or included)

assembly definition hierarchy

In the multi-level folder structure, the Script Compilation belongs to the corresponding assembly definition file according to the shortest path

For example:

There are three paths:




In this way, all scripts in the folder example2 will be compiled into the assembly definition file example2

All scripts in the folder example1 are compiled into the assembly definition file example1

All other scripts in the scripts folder but not in example1 and example2 will be compiled into the assembly definition file main

assembly definition file is not an assembly generation file

The assembly definition file is not an assembly generation file. It does not support the rules of conditional generation in the generation system. This is also the reason why the assembly definition file does not support setting preprocessing instructions, because these preprocessing instructions are always static

backward compatible implicit dependency

The assembly definition file is backward compatible with the definition compilation system in unity. This means that predefined assemblies always depend on the assembly of each assembly definition file. This is similar to how all scripts in unity depend on all the precompiled assemblies (plugins /. DLLs) that are compatible with the target

The following figure illustrates the dependencies between predefined assemblies and assembly definition files assemblies and precompiled assemblies:

The priority of the assembly definition file set in unity is higher than that of the predefined compilation system. This means that if there are some pre-defined special folders in the assembly definition file (such as resources, editor, plugins, etc.) in the compilation system, they will only exist as ordinary folders and will not have any special meaning

strongly recommend:

Either use the assembly definition file for all scripts in the project, or none at all. Otherwise, scripts that do not use the assembly definition file will still be recompiled after modification, which is a waste of time. This makes the script that uses the assembly definition file meaningless

If we open the assembly definition file, we will find that it is actually a JSON file

It contains name, references, include platforms and exclude platforms fields

All of them are optional except name

Well, that’s all for the assembly definition file. After we understand it, we must try to use it in the project, so that we can better grasp and play its role

To be continued

Come on~

welcome scan code to pay attention to official account, get continuous update

Dear guests, if you think it’s useful, please like it and share it~

this article is shared by WeChat official account – game developer development and sharing (No_ 2SeeYou)。

Similar Posts: