- Home /
How can i use C#'s Conditional Attribute in Unity iPhone?
I'd like to try and make some code debug only, as such i'd like to remove as much of it as possible when i'm making a release build. According to this page
http://weblogs.asp.net/tgraham/archive/2004/03/12/88682.aspx
C# has just such a thing to do this, in its Conditional attributes. Yet i've tried this in unity iPhone and it doesn't seem to compile. I'm not sure whether this works in standard unity or not with its more up to date mono implementation, but i've asked elsewhere what version of mono unity iphone uses to try and track down this problem.
I've tried
#define ENABLED
...
...
[ConditionalAttribute("ENABLED")] // doesn't seem to compile :(
static public void DebugFunc(object val) {}
I've also tried using ConditionalAttribute("ENABLED") and Conditional("ENABLED"), neither working.
Anyone got any idea why this doesn't work? It could be VERY VERY helpful!!!
Cheers
Answer by azupko · Jan 11, 2010 at 06:35 PM
Conditionals do work with mono (at least with 2.6.1) but I have never tried with iPhone. #if #endif definitely do work however... not sure if that helps...
Answer by jimbobuk · Mar 06, 2010 at 12:21 AM
I seem to be able to make this work by doing this
using ConditionalAttribute = System.Diagnostics.ConditionalAttribute;
not sure whether this is since the latest 1.6 version of Unity iPhone or not.
I just can't tell whether this will compile out any calls to the function at the callee point. ie. if you are calculating strings as params will these whole steps be skipped if conditional attribute is used to remove the function you are calling.
I tested it. So created some simple cases and then inspected the IL code. It turns out that even though I am calling non-constant functions, it will be stripped out. All arguments and any calculations/function calls done in the arguments will be stripped. A bit unexpected, but it's useful.
Using the Conditional Attribute removes the call to the method it's used on, which is why the calculations/function calls done in the arguments are stripped out. Extremely useful indeed!