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. Dismiss Notice

Right Directive for 'don't include in production'? #if UNITY_EDITOR?

Discussion in 'Scripting' started by frosted, Jul 28, 2014.

  1. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Is #if UNITY_EDITOR the correct directive for this kind of situation?

    If I do something like:
    Code (csharp):
    1.  
    2. #if UNITY_EDITOR
    3. public void Update(){ Debug.Log("Tons of log entries"); }
    4. #endif
    5.  
    Will that guarantee that builds to any other platform exclude the method?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    If you just want to turn off logs in builds, disable the "User player log" option in the player settings rather than doing this.

    --Eric
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    To answer the question about the directive specifically - yes.
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    I might be blind, but I think this option disappear in 4.2 or 4.3... I sure can't find it in 4.3.4 or 4.5, unless it's not in Player Settings anymore.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Nope, still there.

    playerlog.png

    --Eric
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Ah! Only available for standalone... Not available for iOS, Android, Blackberry, WinPhone, etc...
     
    StarManta likes this.
  7. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    4,044
    Is there a better way to do this kind of "only for debug" directive - or is UNITY_EDITOR generally the preferred way?
     
  8. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    There is also Debug.isDebugBuild, which is not a compiler directive. This is true in the editor and if "Development Build" is checked when building.

    Also !Application.isPlaying is what I use if I really want an editor script to not play during gameplay.

    Then #if UNITY_EDITOR.

    Can't really think of anything else...
     
  9. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    There's an undocumented (therefore not guaranteed to stick around) compiler define called DEVELOPMENT_BUILD which gets set when the development tick box is selected. So:

    #if DEVELOPMENT_BUILD
    Debug.Log("development build");
    #endif

    is a way to have code executed outside of a release.