Combining C#'s ConditionalAttribute Conditions with AND and OR
Hello. I'm Hirayama from the Technology Department.
Today's post is a small, unglamorous tip with no figures and no sample project. It's about a C# language feature, so it isn't limited to Unity, but the examples should be easy to follow for Unity folks.
There is an article on the same thing, so if you're comfortable with English, feel free to read that one instead.
Conclusion
#if !(UNITY_EDITOR || UNITY_STANDALONE)
[Conditional("YOUR_PROJECT_NAME_NEVER_DEFINED_SYMBOL")]
#endif
void DebugLog(){ ... }
Writing it like this lets you use Conditional with an AND or OR of several symbols (here UNITY_EDITOR and UNITY_STANDALONE). That's the whole story.
The rest of this post covers why you'd want to do this, and why it works.
What this is about
using System.Diagnostics;
[Conditional("DEBUG")]
void DebugLog(){ ...(body)... }
Most of you probably do this kind of thing a lot, right? The Conditional attribute. It's handy, because you can strip out platform-specific features or debug features without touching the callers.
If the Conditional attribute didn't exist, you'd be stuck wrapping every single place that calls this function in an #if, like this:
#if DEBUG
DebugLog("BUGだー!!!");
#endif
Then again, if it didn't exist, you'd probably just change the original DebugLog() like this:
void DebugLog()
{
#if DEBUG
...(body)...
#endif
}
so that its body is empty when DEBUG isn't defined. That would let you leave the callers alone just the same, so I
suspect it would actually be fine. There may be a difference in optimization, though. It would be nice if not only the
call itself disappeared, but also the cost of preparing the arguments, here the string BUGだー!!! (I haven't
checked). If that's the case, it's great, because you get the same effect as using macros in C/C++.
I want AND and OR
Handy as the Conditional attribute is, sometimes you want to combine several symbols.
"If UNITY_EDITOR or UNITY_STANDALONE is defined" is a pretty common case, I think: development-only features get turned on in the editor or in a PC build for development, that sort of thing. You can of course simply remove the function like this:
#if UNITY_EDITOR || UNITY_STANDALONE
void DebugLog(){ ... }
#endif
but then you have to touch the callers too. That's a pain, so you want to somehow join several symbols with || or && on the function definition side.
That said, you can't do this:
#if UNITY_EDITOR || UNITY_STANDALONE
#define USE_DEBUG_LOG
#endif
[Conditional("USE_DEBUG_LOG")]
void DebugLog(){ ... }
It feels like it would work, but it doesn't. That's because the Conditional attribute doesn't mean "the function exists only when the condition is met"; it means "the function is called only when the condition is met."
In other words, the decision is made on the calling side, and it depends on whether the symbol in question is present in the caller's code.
In the example above, USE_DEBUG_LOG also has to exist in the caller's code, so for calls from a different file, DebugLog ends up being called whether or not UNITY_EDITOR or UNITY_STANDALONE is there. That leads to accidents like "logs are showing up in the product!!!"
This is where the snippet from the top of this post comes in.
#if !(UNITY_EDITOR || UNITY_STANDALONE)
[Conditional("YOUR_PROJECT_NAME_NEVER_DEFINED_SYMBOL")]
#endif
void DebugLog(){ ... }
When I first saw this, I didn't get the intent right away, but it makes sense once you go through the cases.
- If neither UNITY_EDITOR nor UNITY_STANDALONE is there, the Conditional appears.
- Other source files see these symbols too, so the condition applies there as well and the function is no longer called.
- If UNITY_EDITOR or UNITY_STANDALONE is there, the Conditional disappears.
- This function is always called.
Because the condition has to be inverted with !, it's a little hard to follow, but either way it achieves "DebugLog is
called only when UNITY_EDITOR or UNITY_STANDALONE is there."
YOUR_PROJECT_NAME_NEVER_DEFINED_SYMBOL can be anything, as long as it's "a symbol that will never be defined." I use
the project's name for YOUR_PROJECT_NAME, but maybe I'm being overly cautious. Something like NEVER_DEFINED_SYMBOL
might be enough. If you're confident you'll never write
#define X
then even the single letter X would do. Not that I really recommend it.