Version: 2023.1
Conditional Compilation
程序集定义

Custom scripting symbols

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, via scripting, or via an asset file.

Setting scripting symbols via the 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 scripts 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.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.

Setting scripting symbols in batch mode

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.

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.

Note: If you want to modify only global scripting symbols, add them to the Scripting Define Symbols in the Editor via the Player Settings 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.

Conditional Compilation
程序集定义