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.
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.