Conditional compilation using the conditional attribute of C # with the Unity Editor Macro command

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.