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 InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary.
In the Define Constraints section, click the + button to add a new symbol to the list of constraints.
Enter the symbol name.
You can negate the symbol by putting an exclamation point in front of the name. For example, the constraint, !UNITY_WEBGL
would include the assembly when UNITY_WEBGL
was NOT defined.
Click Apply.
You can use the following symbols as constraints:
Symbols defined in scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary are not considered when determining if a constraint has been satisfied.
See Define Constraints for additional information.
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:
The Expression outcome shows which versions the expression evaluates to. If the outcome displays, Invalid, then expression syntax is incorrect.
The following example defines the symbol, USE_TIMELINE_1_3
, if the project uses Timeline 1.3 and defines, USE_NEW_APIS
, if the project is opened in Unity 2021.2.0a7, or later:
Click Apply.
Symbols defined in the Assembly Definition are only in scope for the scripts in the assembly created for that definition.
Note that you can use the symbols defined using the Version Defines list as Define Constraints. Thus you could specify that an assembly should only be used when specific versions of a given package are also installed in the project.
You can use expressions to specify an exact version or a range of versions. A Version Define expression uses mathematical range notation.
A square bracket, “[]” designates that the range includes the endpoint:
[1.3,3.4.1]
evaluates to1.3.0 <= x <= 3.4.1
A parenthesis “()” designates that the range excludes the endpoint:
(1.3.0,3.4)
evaluates to1.3.0 < x < 3.4.0
You can mix both range types in a single expression:
[1.1,3.4)
evaluates to1.1.0 <= x < 3.4.0
(0.2.4,5.6.2-preview.2]
evaluates to0.2.4 < x <= 5.6.2.-preview.2
You can use a single version designator in square brackets to designate an exact version:
[2.4.5]
evaluates tox = 2.4.5
As a shortcut, you can enter a single version without range brackets to indicate that the expression includes that version or later:
2.1.0-preview.7
evaluates tox >= 2.1.0-preview.7
Note: No spaces are allowed in an expression. No wildcard characters are supported.
Current versions of Unity (and all versions that support Assembly Definitions) use a version designator with three parts: MAJOR.MINOR.REVISION, for example, 2017.4.25f1
, 2018.4.29f1
, and 2019.4.7f1
.
2022.x.z
and earlier, or the major technical version from Unity 6 (6000.x.z) onwards.Release type designators are compared as follows:
a < b < f = c < p < x
In other words, an alpha release is considered earlier than a beta, which is earlier than a normal (f) or China (c) 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.
Unity version numbers are allowed to have a suffix after the REVISION component, such as 2019.3.0f11-Sunflower
. Any suffixes are ignored for the purpose of comparing versions.
As an example, the following expression includes any 2017 or 2018 version of Unity, but not any version in 2019 or later:
[2017,2019)
Package and module version designators have four parts, following the Semantic Versioning format: MAJOR.MINOR.PATCH-LABEL. The first three parts are always numbers, but the label is a string. Unity packages in preview use the string, preview
or preview.n
, where n > 0
.
See Package Versioning for more information about package version numbers.
For example, the following expression includes all versions of a package with MAJOR.MINOR versions between 3.2 and 6.1 (inclusive):
[3.2,6.1]