Profile
In a traditional C # project, when you use the conditional feature for conditional compilation, you need to add conditional compilation symbols to the properties of the project in Visual Studio, and use reference to this article.
In a Unity project, conditional compilation symbols need to be added in the Unity Editor editor.
Why to use conditional compilation
The most common use of conditional compilation in unity is to compile different pieces of code on a sub-platform. Because unity is cross-platform and different platforms have different features, we often see #if ... #endif to do conditional compilation of code blocks.
Another common use is to output logs when running under the editor, and to turn off the log in order to save performance when packaging a real-machine debug (output logs per frame in the update () function consumes performance). At this point, the output log function can be marked with the conditional attribute, only in the Unity editor to open the specified macro command, the output log function can be compiled, it is convenient to switch the log.
Example
namespace unityengine{ public static class debugextension {[Conditional ( "" Span style= "COLOR: #800000" >enablelog " )] public static void Logger (this object obj, string message) {Debug.Log (messag e); } }}
The above code, through the extension method of C #, adds a logger () method to all the System.Object classes, which is marked as [Conditional ("enablelog")] . You need to add the enablelog Macro command in the Unity editor to compile the method.
The Unity Editor Add Macro command in File-build settings-player Settings-other Settings, such as.
After entering Enablelog , the above logger () method can be compiled. If you remove enablelog, all code that calls the logger () function will not be compiled!
this. Logger (" methods in the log extension class. This method needs to add the macro command in the Unity Editor Enablelog to be compiled! ");
Conditional compilation using the conditional attribute of C # with the Unity Editor Macro command