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.
For more information, refer to Define Constraints.
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
.
For more information on package version numbers, refer to Versioning.
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]
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.