Unity Predefined assemblies/assembly definition files

使用Unity2018 的TestRunner,无法引用自己的代码进行测试,原因是测试代码和程序代码不在同一个程序集,无法引用;需要新建程序集,进行引用。

Script compilation and assembly definition files

About

Unity automatically defines how scripts
 compile to managed assemblies. Typically, compilation times in the Unity Editor for iterative script changes increase as you add more scripts to the Project.

Use an assembly definition file to define your own managed assemblies based upon scripts inside a folder. To do this, separate Project scripts into multiple assemblies with well-defined dependencies in order to ensure that only required assemblies are rebuilt when making changes in a script. This reduces compilation time. Think of each managed assembly as a single library within the Unity Project.

Unity Predefined assemblies/assembly definition files

Figure 1 - Script compilation

Figure 1 above illustrates how to split the Project scripts into several assemblies. Changing only scripts in Main.dll causes none of the other assemblies to recompile. Since Main.dll contains fewer scripts, it also compiles faster than Assembly-CSharp.dll. Similarly, script changes in only Stuff.dll causes Main.dll and Stuff.dll to recompile.

How to use assembly definition files

Assembly definition files are Asset files that you create by going to Assets
 > Create > Assembly Definition. They have the extension .asmdef.

Add an assembly definition file to a folder in a Unity Project to compile all the scripts in the folder into an assembly. Set the name of the assembly in the Inspector
.

Note: The name of the folder in which the assembly definition file resides and the filename of the assembly definition file have no effect on the name of the assembly.

Unity Predefined assemblies/assembly definition files

Figure 2 - Example Import Settings

Add references to other assembly definition files in the Project using the Inspector too. To view the Inspector, click your Assembly Definition file and it should appear. To add a reference, click the + icon under the References section and choose your file.

Unity uses the references to compile the assemblies and also defines the dependencies between the assemblies.

To mark the assembly for testing, enable Test Assemblies in the Inspector. This adds references to unit.framework.dll and UnityEngine.TestRunner.dll in the Assembly Definition file.

When you mark an assembly for testing, makes sure that:

  • Predefined assemblies (Assembly-CSharp.dll etc.) do not automatically reference Assembly Definition Files flagged for testing.

  • The assembly is not included in a normal build. To include the assemblies in a player build, use BuildOptions.IncludeTestAssemblies in your building script. Note that this only includes the assemblies in your build and does not execute any tests.

Note: If you use the unsafe keyword in a script inside an assembly, you must enable the Allow ‘unsafe’ Code option in the Inspector. This will pass the /unsafe option to the C# compiler when compiling the assembly.

https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html

Predefined assemblies:

Unity compiles scripts
 in four separate phases, based on where the script file is located within the project folder structure. Unity creates a separate CSharp project file (.csproj) and a predefined assembly for each phase. (If there are no scripts eligible for a compilation phase, Unity does not create the corresponding project file or assembly.)

Compilation order is significant when a script references a class compiled in a different phase (and therefore located in a different assembly). The basic rule is that anything that is compiled in a phase after the current one cannot be referenced. Anything that is compiled in the current phase or an earlier phase is fully available.

https://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html