Version: Unity 6 Preview (6000.0)
Language : English
Unity scripting symbol reference
Test conditional compilation

Custom scripting symbols

In addition to Unity’s built-in scripting symbols, you can specify your own custom scripting symbols, either using the Unity Editor, via scripting, or via an asset file.

Setting scripting symbols via the Unity Editor

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.

The Scripting Define Symbols settings in the Project Settings window. This example shows two custom symbols defined in the list.
The Scripting Define Symbols settings in the Project Settings window. This example shows two custom symbols defined in the list.

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.

Defining scripting symbols via script

You can use the following API to define scripting symbols:

Setting scripting symbols for Editor script compilation

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.SetScriptingDefineSymbols. However, there are some important details to note about how this operates.

Important: Calling this method from script does not immediately apply the change and recompile your scripts. For your new symbols to take effect, control must be returned to the Editor, which then asynchronously reloads the scripts and recompiles them based on your new symbols and the directives which act on them.

For example, if you use this method in an Editor script and immediately call BuildPipeline.BuildPlayer on the next line, Unity is still running your Editor scripts with the old set of scripting symbols because they haven’t been recompiled yet. 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.

Setting scripting symbols in batch mode

When the Editor runs in batch mode, it runs headless, so there is no Editor loop to trigger recompilation of scripts. You should not use Editor scripts to set scripting symbols in batch mode on a continuous integration (CI) server. The scripts will not be recompiled and your changes will not be applied.

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.

Setting scripting symbols via an asset file

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.

Additional resources

Unity scripting symbol reference
Test conditional compilation