C# directives allow you to selectively include or exclude code from compilation, based on whether certain scripting symbols are defined or not defined.
In addition to the built-in scripting symbols (relating to Platform, the Editor Version, and other miscellaneous system environment scenarios), you can specify your own custom scripting symbols, either using the Editor UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary, via scripting, or via an asset file.
To set or remove define directives via the Editor, go to Edit > Project Settings > Player. Then in the Other Settings panel, scroll down to Script Compilation.
You can add and remove your own custom scripting symbols to the Scripting Define Symbols list by using the + and - buttons, and typing the name of your new symbols into the fields. When you select Apply, your new scripting symbols are applied, and Unity recompiles the 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 in your project using these new symbols.
The Copy Defines button copies the current set of custom scripting symbols from the list into your clipboard as a string of semicolon-separated values.
You can use the following API to define scripting symbols:
If you need to define scripting symbols via scripts in the Editor so that your Editor scripts are affected by the change, you must use PlayerSettings.SetScriptingDefineSymbolsForGroup. However, there are some important details to note about how this operates.
Important: this method does not take immediate effect. Calling this method from script does not immediately apply and recompile your scripts. For your directives to take effect based on a change in scripting symbols, you must allow control to be returned to the Editor, where it then asynchronously reloads the scripts and recompiles them based on your new symbols and the directives which act on them.
So, for example, if you use this method in an Editor script, then immediately call BuildPipeline.BuildPlayer
on the following line in the same script, at that point Unity is still running your Editor scripts with the old set of scripting symbols, because they have not yet been recompiled with the new symbols. This means if you have Editor scripts which run as part of your BuildPlayer execution, they run with the old scripting symbols and your player might not build as you expected.
The asynchronous nature of Unity’s compilation mentioned above is also important to understand if you are writing Editor scripts which will run in batch mode in the Unity Editor on a continuous integration (CI) server. This is because when the Editor runs in batch mode, it runs “headless”, so there is no editor loop which would cause it to recompile with the new scripting symbols. Because of this, you should not use Editor scripts to set scripting symbols within a batch mode CI server because the scripts will not be recompiled, so they will not be applied.
Instead, if you need specific symbols to be defined in an Editor running in batch mode, you must ensure the editor is launched with the correct symbols defined from the start. You can do this by specifying the symbols using a csc.rsp asset file instead of using editor scripts, as described below.
You can set custom scripting symbols via a text asset in your project. To do this you must add a text file that defines the custom scripting symbols named csc.rsp
in the root of your project’s Assets folder. This special file is read by Unity at startup and applied before any code is compiled.
As an example, if you include the single line -define:UNITY_DEBUG in your csc.rsp file, the symbol UNITY_DEBUG is included as a globally defined scripting symbol for C# scripts, except for Editor scripts.
Every time you make changes to .rsp files, you need to recompile in order for them to be effective. You can do this by updating or reimporting a single script file.
Note: If you want to modify only global scripting symbols, add them to the Scripting Define Symbols in the Editor via the Player SettingsSettings that let you set various player-specific options for the final game built by Unity. More info
See in Glossary window (as described above), because this covers all the compilers. If you choose the .rsp files instead, you need to provide one file for every compiler Unity uses.
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.