Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Do conditional code branches get optimized out when building apps for deployment?

Discussion in 'Scripting' started by maxxjr, Aug 1, 2020.

  1. maxxjr

    maxxjr

    Joined:
    Dec 10, 2017
    Posts:
    13
    If I do something like this:

    public bool featureEnabled; // so that feature can be enabled/disabled in UnityEditor
    void Update(){
    if (featureEnabled){
    doFeature();​
    }​
    }

    if i set featureEnabled to false in UnityEditor, and then build the program for deployment, will the compiler remove the check for featureEnabled? In other words, is the optimization smart enough to determine that featureEnabled will always be false, and just remove the check and the block of code in the if statement altogether? (I would expect traditional C/C++ compilers to do this optimization. No idea if C# will do this)

    Thanks!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
  3. maxxjr

    maxxjr

    Joined:
    Dec 10, 2017
    Posts:
    13
    Hi PB,

    Thanks again.

    I understand the use of #defines from the C/C++ world.

    For rapid prototyping, I was thinking it would be easier just to check/uncheck the box for the feature in the Unity Editor, and rebuild to test something out, vs. modifying a text file to enable/disable the feature with a #define.

    I guess with the #define route, I should create an include file with all of the #defines, and include this with every other file.