Does Unity support C# using precompiler directives?
Like:
#if DEBUG_NULL_CHECKS
if (null == myObject)
{
Debug.Log("Unexpected null == myObject);
}
#endif
If so how do you define the precompiler directives in Unity?
With VS 2008 it would just be a “Conditional compilation symbols:” in the project settings → debug tab.
There seems to be support for an iPhone conditional compilation symbol:
UNITY_IPHONE
according to this post:
http://forum.unity3d.com/viewtopic.php?t=19203&sid=e6b333476cef73d1e54e49554eafaa96
How do you add more?
There is no UI for it, but I would expect that #define is supported. I’ve only used it outside the editor though.
I’ll have to do a test, to see how a define could work if the compile order is random.
I’m also curious about this. I was using the timeline editor for some of my projects, and now that it’s gone I have to go back and forth between 2.5 and 2.1, the problem with this is that I can’t really use any of the new editor related scripting because I get a lot of errors when I open my project in 2.1.
Something like…
#ifdef UNITY25
/// all my editor scripting code here
#endif
This will make my life soo much easier
Here is an example test project that shows #define does not work.
When you play I use the #if check to show a GUI.Label. None of the classes that use the #if check can detect the #define.
#define DO_THE_DEFINE
public class DefineClass
{
}
using UnityEngine;
using System.Collections;
public class ClassA : MonoBehaviour
{
void GetDefined(DefineClass defineClass)
{
}
void OnGUI()
{
#if DO_THE_DEFINE
GUI.Label(new Rect(0, 0, 200, 20), "Class A has DO_THE_DEFINE");
#else
GUI.Label(new Rect(0, 0, 200, 20), "Class A lacks DO_THE_DEFINE");
#endif
}
}
136890–5028–$precompiler_177.zip (1.09 MB)
#define’s in C# do not work this way. If you declare a #define at the top of a module, it is only visible within that module. Other modules will not be able to see it. Therefore, you would need to place the #define in every module where it was needed.
Normally a #define is specified within the Visual Studio Project settings in order for that #define to be visible across the entire project. As far as I know, there is no possible way to specify a #define from within the Unity GUI. (Hey Unity Tech, can you guys add this feature?)