Conditionally including assemblies
Use scripting symbols to conditionally include or exclude assemblies from compilation.
Read time 4 minutesLast updated 12 days ago
You can use preprocessor symbols to control whether an assembly is compiled and included in builds of your application (including Play mode in the Editor). You can specify which symbols must be defined for an assembly to be used with the Define Constraints list in the Assembly Definition options:
- Select the Assembly Definition for the assembly to view its properties in the .Inspector
?
- In the Define Constraints section, click the + button to add a new symbol to the list of constraints.
- Enter the symbol name.
- Click Apply.
For more information on which scripting symbols you can use for define constraints, refer to Define Constraints.
Defining symbols based on Unity and project package versions
If you need to compile different code in an assembly according to whether a project uses specific versions of Unity or of a package, you can add entries to the Version Defines list. This list specifies rules for when a symbol should be defined. For version numbers, you can specify a logical expression that evaluates to a specific version or a range of versions.
To conditionally define a symbol:
-
Select the Assembly Definition asset for the assembly to view its properties in the Inspector.
-
In the Version Defines section, click the + button to add an entry to the list.
-
Set the properties:
- If resource: Choose Unity or the package or module that must be installed in order for this symbol to be defined.
- version is: An expression that evaluates to a specific version or a range of versions. Refer to Version Define expressions for the syntax rules.
- set define: The scripting symbol to define when the version conditions are met.
The Version expression outcome shows which versions the expression evaluates to. If the outcome displays, Invalid, then the expression syntax is incorrect. -
Click Apply.
Symbols defined in the Assembly Definition are only in scope for the scripts in the assembly created for that definition.
Version define expressions
You can use expressions to specify an exact version or a range of versions.
A version expression uses mathematical interval notation, where square brackets designate that the range includes the endpoint and parentheses designate that the range excludes the endpoint.
[]()The following table illustrates the syntax:
Expression | Evaluates to |
|---|---|
| |
| |
| |
| |
| |
| |
Unity versioning scheme
Current versions of Unity (and all versions that support Assembly Definitions) use a version designator with three parts: , for example, .
MAJOR.MINOR.REVISION6000.0.33f1-
Theversion is four digits representing the release year for Unity
MAJORand earlier, or the major technical version from Unity 6 (2022.x.z) onwards.6000.x.z -
Theversion is the target release quarter, such as
MINOR,1,2, or3.4 -
Thedesignator has three parts of its own, with the format:
REVISION, where:RRzNN-
is a one or two digit revision number
RR -
is a letter designating the release type:
z- = alpha release
a - = beta release
b - = a normal public release
f - = China release version (equivalent to f)
c - = patch release
p - = experimental release
x
-
is a one or two digit incremental number
NN
-
Release type designators are compared as follows:
a < b < f = c < p < x
An alpha release is considered earlier than a beta, which is earlier than a normal () or China () release. A patch release is always later than a normal or China release with the same revision number and an experimental release is later than any other release type. Note that experimental releases do not use an incremental number at the end.
fcThe following example expression includes any 6000.0 or 6000.1 version of Unity, but not any version in 6000.2 or later:
[6000.0,6000.2)
Package and module version numbers
Package and module version designators have four parts, following the Semantic Versioning format: . The first three parts are always numbers, but the label is a string. Unity packages in preview use the string, or , where .
MAJOR.MINOR.PATCH-LABELpreviewpreview.nn > 0The following example expression includes all versions of a package with versions between 3.2 and 6.1 (inclusive):
MAJOR.MINOR[3.2,6.1]