Version: 2021.2
程序集定义
程序集定义引用属性

程序集定义 (Assembly Definition) 属性

单击程序集定义资源 (Assembly Definition Asset) 可以在 Inspector 窗口中设置程序集的属性。

程序集定义属性分为以下部分:

Name and General

属性: 描述:
Name The name for the assembly (without a file extension). Assembly names must be unique across the Project. Consider using a reverse-DNS naming style to reduce the chance of name conflicts, especially if you want to use the assembly in more than one Project.

Note: Unity uses the name you assign to the Assembly Definition asset as the default value of the Name field, but you can change the name as needed. However, if you reference an Assembly Definition by its name rather than its GUID, changing the name will break the reference.
Allow ‘unsafe’ Code 如果在程序集内的脚本中使用了 C# unsafe 关键字,请启用 Allow ‘unsafe’ Code 选项。启用此设置后,Unity 在编译程序集时会将 /unsafe 选项传递到 C# 编译器。
Auto Referenced 指定预定义的程序集是否应引用此项目程序集。禁用 Auto Reference 选项后,Unity 不会在编译过程中自动引用该程序集。这不会影响 Unity 是否将文件包含在构建中。
No Engine References 启用此属性后,Unity 在编译程序集时不会添加对 UnityEditor 或 UnityEngine 的引用。
Override References Enable the Override References setting to manually specify which precompiled assemblies this assembly depends upon. When you enable Override References, the Inspector shows the Assembly References section, which you can use to specify the references.

A precompiled assembly is a library compiled outside your Unity Project. By default, assemblies you define in your Project reference all the precompiled assemblies you add to the Project, which matches how the predefined assemblies reference all precompiled assemblies. When you enable Override References, this assembly only references the precompiled assemblies you add under Assembly References.

Note: To prevent Project assemblies from automatically referencing a precompiled assembly, you can disable its Auto Referenced option. See Plugin Inspector for more information.
Root Namespace The default namespace for scripts in this assembly definition. If you use either Rider or Visual Studio as your code editor, they automatically add this namespace to any new scripts you create in this assembly definition.

For more information, see Creating an Assembly Definition asset.

Define Constraints

Define constraints specify the compiler #define directives that must be defined for Unity to compile or reference an assembly.

Unity only compiles and references a Project assembly if all of the Define Constraints are satisfied. Constraints work like the #if preprocessor directive in C#, but on the assembly level instead of the script level. Define all the symbols in the Define Constraints setting for the constraints to be satisfied.

要表示必须取消定义某个符号,请在该符号前面添加否定!(叹号)符号作为前缀。例如,如果在 Define Constraints 中指定以下符号:

  • !ENABLE_IL2CPP
  • UNITY_2018_3_OR_NEWER

The constraints are satisfied when the symbol ENABLE_IL2CPP is not defined and the symbol UNITY_2018_3_OR_NEWER is defined. The result is that Unity only compiles and references this assembly on non-IL2CPP scripting runtimes for Unity 2018.3 or newer.

You can use the || (OR) operator to specify that at least one of the constraints must be present in order for the constraints to be satisfied. For example:

  • UNITY_IOS || UNITY_EDITOR_OSX
  • UNITY_2019_3_OR_NEWER
  • !UNITY_ANDROID

The constraints are satisfied when either UNITY_IOS or UNITY_EDITOR_OSX and UNITY_2019_3_OR_NEWER are defined and UNITY_ANDROID is not defined. Individual lines are analogous to performing a logical AND operation between the constraints in them. The above example is equivalent to:

(UNITY_IOS OR UNITY_EDITOR_OSX) AND (UNITY_2019_3_OR_NEWER) AND (NOT UNITY_ANDROID)

可以使用 Unity 的任何内置 #define 指令、全局编译器响应文件 (.rsp) 定义的符号,以及项目的 Scripting Define Symbols Player 设置中定义的任何符号。请参阅[平台相关的编译]以了解更多信息(包括内置符号的列表)。

Note: The Scripting Define Symbols settings are platform-specific. If you use this setting to define whether Unity should use an assembly, make sure that you define the necessary symbols on all the relevant platforms.

For more information, see Conditionally including an assembly.

无效或不兼容的约束

Unity marks each constraint with an indicator based on the currently defined settings (for example, the following set of three constraints indicates that the first symbol is currently defined while the other two are not). Since each individual constraint must be true for the overall constraint to be satisfied, the Editor marks the entire Define Constraints section as currently incompatible or invalid.

Invalid or incompatible constraints are flagged.
Invalid or incompatible constraints are flagged.

To satisfy the constraints in this example, you could change the Scripting Backend to IL2CPP for the second constraint (in Player Settings) and remove the invalid characters from the third constraint. However, what often matters is how the constraints are evaluated when you build the project, not how the constraints appear in the Unity Editor (for example, you might have an assembly that you only want to include in builds that use the IL2CPP backend, but not on other builds that use the Mono backend).

Assembly Definition References

属性: 描述:
Assembly Definition References Specify references to other assemblies that you have created using Assembly Definition assets. Unity uses these references to compile the assembly and also define the dependencies between assemblies.
Use GUIDs 此设置控制 Unity 如何序列化对其他程序集定义资源的引用。启用此属性后,Unity 将这些引用另存为资源的 GUID,而不是程序集定义的名称。最好是使用 GUID 而不是名称,因为这意味着可以更改程序集定义资源的名称,而不必更新引用该程序集的其他程序集定义文件。

For more information, see Creating an Assembly Definition asset

Assembly References

The Assembly References section only appears when you enable the Override References property (in the General section). Use this area to specify any references to precompiled assemblies on which this assembly depends.

For more information, see Referencing a precompiled, plugin assembly

Platforms

设置程序集的平台兼容性。Unity 仅在包含(或不排除)的平台上编译或引用此程序集。

For more information, see Creating a platform-specific assembly

版本定义

根据项目中包和模块的版本指定要定义的符号。

属性: 描述:
Resource 一个包或模块
Define 该符号定义 Resource 的适用版本何时也出现在此 Unity 项目中。
Expression An expression defining a version or range of versions. See Version Define expressions.
Expression outcome The Expression evaluated as a logical statement, where “x” is the version checked. If the Expression outcome says, Invalid, then the Expression is malformed.

For more information, see Defining symbols based on project packages

程序集定义
程序集定义引用属性