Conditional attribute ignores #defines in 5.5

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.

#if UNITY_EDITOR || DEVELOPMENT_BUILD
#define ENABLED
#endif

public class MyClass
{
        [Conditional("ENABLED")]
        public static void MyFunc() { }

}

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.

1 Like

Anyone?

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?

1 Like

That’s one of the things I use it for too.

I now do this.

[Conditional("UNITY_EDITOR"),Conditional("DEVELOPMENT_BUILD")]
public static void Log(...)
{
    ...
}

Ha, so it’s only custom defines that don’t work? Thanks for the tip; I’ll try changing it over on Monday

Did you send a bug report?

Hi @Baste ,

Yep, it’s #884630: https://fogbugz.unity3d.com/default.asp?884630_k7co9g93vts7bgta

With some quick further testing, it only seems to affect the [Conditional] attribute; if you use the custom define in a normal manner (i.e. wrapping code in an #if/#endif block), it’s fine

Hello, I stumble upon this problem myself, Made a report and got a feedback


reply’s link: https://msdn.microsoft.com/en-us/library/aa664622(v=vs.71).aspx

So now you have to use #define where you call your [conditionals] funcs instead of where you defined those [conditionals]

1 Like

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?

1 Like

Have you tried setting them in Player Settings (Scripting Define Symbols)?

@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.

1 Like

Too bad we have a computed constant, the PlayerSettings won’t work.