1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

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,195
    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.
     
  2. andymads

    andymads

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

    Nicolas1212

    Joined:
    Dec 18, 2014
    Posts:
    97
    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?
     
  4. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,195
    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:
    97
    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:
    2,093
    Did you send a bug report?
     
  7. Nicolas1212

    Nicolas1212

    Joined:
    Dec 18, 2014
    Posts:
    97
  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:
    97
    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?
     
  10. andymads

    andymads

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

    Nicolas1212

    Joined:
    Dec 18, 2014
    Posts:
    97
    @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.