Search Unity

Conditional attribute ignores #defines in 5.5

Discussion in 'Scripting' started by andymads, Dec 20, 2016.

  1. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    In 5.5 it seems that if you #define an identifier, you can no longer use the identifier with the conditional attribute.

    E.g.

    The identifier ENABLED is defined but ignored by the conditional attribute.

    Code (CSharp):
    1. #if UNITY_EDITOR || DEVELOPMENT_BUILD
    2. #define ENABLED
    3. #endif
    4.  
    5. public class MyClass
    6. {
    7.         [Conditional("ENABLED")]
    8.         public static void MyFunc() { }
    9.  
    10. }
    I found this report on Issue Tracker which says that it's by design. So why was this changed? I haven't seen a mention of this change in release notes.
     
    nick-morhun likes this.
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Anyone?
     
  3. Nicolas1212

    Nicolas1212

    Joined:
    Dec 18, 2014
    Posts:
    139
    Chiming in here as we're having the same issue. We use this very feature to remove Debug logs from production builds (which gives a big speed/memory improvement). Any word on why it was removed?
     
    nick-morhun likes this.
  4. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    That's one of the things I use it for too.

    I now do this.

    Code (CSharp):
    1. [Conditional("UNITY_EDITOR"),Conditional("DEVELOPMENT_BUILD")]
    2. public static void Log(...)
    3. {
    4.     ...
    5. }
     
  5. Nicolas1212

    Nicolas1212

    Joined:
    Dec 18, 2014
    Posts:
    139
    Ha, so it's only custom defines that don't work? Thanks for the tip; I'll try changing it over on Monday
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,334
    Did you send a bug report?
     
  7. Nicolas1212

    Nicolas1212

    Joined:
    Dec 18, 2014
    Posts:
    139
  8. zepedroHSK

    zepedroHSK

    Joined:
    Aug 22, 2013
    Posts:
    5
    Last edited: Apr 18, 2017
    andymads likes this.
  9. Nicolas1212

    Nicolas1212

    Joined:
    Dec 18, 2014
    Posts:
    139
    This kind of makes this the whole Conditional attribute useless.

    We use this for removing Debug logs from production builds. Previously, we had the defines in the class that we call to log the message (essentially a wrapper for Debug.Log... calls), like this:

    #define USE_DEBUG_LOGS
    #define USE_WARN_LOGS
    #define USE_ERROR_LOGS

    Not ideal, but at least it was in one place.

    Based on this change, I now need to include the defines in *every* class, which makes it unusable. Defining them in ClassA doesn't bring it over to ClassB, even though ClassA is created before ClassB.

    Is there any way to globally declare a #define?
     
    nick-morhun likes this.
  10. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Have you tried setting them in Player Settings (Scripting Define Symbols)?
     
  11. Nicolas1212

    Nicolas1212

    Joined:
    Dec 18, 2014
    Posts:
    139
    @andymads has the right of it. From what I could find, this is the only way to globally define something in Unity? Without hacking something I mean. If anyone has another/better way, I'll gladly take it.

    For those that are looking for it, while in the past you might have defined your constants like this:

    #define USE_DEBUG_LOGS
    #define USE_WARN_LOGS
    #define USE_ERROR_LOGS​

    To use PlayerSettings, you enter

    USE_DEBUG_LOGS;USE_WARN_LOGS;USE_ERROR_LOGS​

    into the Scripting Define Symbols box. One added benefit for this system (depending on how you're using your constants) is that you can separate the define per platform, so in our case, editor builds automatically have logs while iOS/Android builds don't.
     
    nick-morhun likes this.
  12. nick-morhun

    nick-morhun

    Joined:
    May 12, 2015
    Posts:
    51
    Too bad we have a computed constant, the PlayerSettings won't work.