Adding diagnostics to C# code
Use the managed code variant setting to include additional diagnostics, debug symbols, or instrumentation in your compiled C# code to help with debugging or profiling a built Player.
Read time 4 minutesLast updated 6 days ago
The project build process compiles managed (C#) project code into managed assemblies and packages them into an application that runs on a particular platform and architecture.
Depending on the build context, you might want to build different variants of managed code, with different kinds of code paths or metadata included to help with debugging or profiling.
You can choose to include the following kinds of additional information in your managed code assemblies:
- Instrumentation: Instrumentation code is included in managed code assemblies when the scripting symbol is defined. Instrumentation is the general term for extra code inserted around your functional code for the purposes of taking observations and measurements. In the context of a Unity project build, it means code related to the Profiler such as profiler markers and most calls to the
UNITY_INCLUDE_INSTRUMENTATIONAPI. For more information, refer to theProfilerAPI reference.Profiler - Assertions: Calls to Unity's API are included in managed code assemblies when the
Assertions.Assertscripting symbol is defined. WhenUNITY_ASSERTIONSisn't defined, the assertion methods are stripped out via theUNITY_ASSERTIONSattribute, so they add no runtime cost in shipping builds. For more information, refer to the[Conditional("UNITY_ASSERTIONS")]API reference.Assertions.Assert - Safety checks: Safety checks are included in managed code assemblies when the scripting symbol is defined. Safety checks are optional runtime validation that check for things such as out-of-bounds access to containers, concurrent data writes, and attempts to call main thread only APIs from a background thread.
UNITY_ENABLE_CHECKS - Debug symbols: Debug symbols are included in managed code assemblies when the scripting symbol is defined. This code is unoptimized and appropriate for stepping through with a debugger.
DEBUG
This kind of additional information makes it easier to debug and profile a built Player but also increases the build size and impacts performance. You usually add it to a development build and omit it from a final release build shipped to customers.
Configuring managed code variants
You can include the different types of information mentioned previously in managed assemblies by configuring the managed code variant for your build profile. Managed code variants are predefined configurations of managed code, which include different levels of additional information, and define the relevant scripting symbols.
The lowest-level variant, Release, includes no additional information. Each additional level includes everything from the level below. The highest-level variant, Debug, includes everything.
To change the managed code variant for your project in the Unity Editor:
- Go to Edit > > Player.Project Settings
?
- In Other Settings, navigate to the Optimization heading.
- Set the Managed Code Variant property to the desired value.
You can also read or set the managed code variant from build scripts with and respectively.
PlayerSettings.GetManagedCodeVariantPlayerSettings.SetManagedCodeVariantThe following table lists the managed code variant options:
Managed Code Variant | Scripting symbols defined | Description | Code optimization |
|---|---|---|---|
| Debug | | Adds debug symbols to the Checked variant. | Off |
| Checked | | Adds safety checks and assertions to the Instrumented variant. | On |
| Instrumented | | Adds instrumentation and enables the C# | On |
| Release | None. | Adds no diagnostics or instrumentation. | On |
Example
For example, the following snippet wraps a and pair so it only exists when instrumentation is enabled. This is only compiled into your managed assemblies when the managed code variant defines the scripting symbol:
Profiler.BeginSampleProfiler.EndSampleUNITY_INCLUDE_INSTRUMENTATION#if UNITY_INCLUDE_INSTRUMENTATION Profiler.BeginSample("MyExpensiveWork");#endif DoExpensiveWork();#if UNITY_INCLUDE_INSTRUMENTATION Profiler.EndSample();#endif
Code variants and Development Builds
In Unity versions before 6.6, selecting the option in the Build Profiles window produced a Player build that included the equivalent of the Checked managed code variant. This is no longer the case, and you must configure the Checked variant if you want a build that includes safety checks.
Development Build
?
Although for the time being the Development Build option still defines and , this is subject to change. It's best practice to use the managed code variant setting to indicate what you want to include in your managed code.
UNITY_ASSERTIONSUNITY_INCLUDE_INSTRUMENTATION