Project wide conditional compilation

In a regular .NET application, it’s possible to define symbols used for conditional compilation at a project level. For example, a debug build of a VS solution defines the symbol ‘DEBUG’.

I know that in Unity it’s possible to #define symbols in C# code at a file level, but I’m wondering if there is a way to define symbols across the entire project?

An example of the purpose of this would be to create a ‘lite’ version of a game that contains less features, and maybe a nag screen.

Thanks!

I would also like to know this.

Yup, do it all the time.

Create a main script called something like GameManager and make it a singleton.

This has been covered quite a bit in the forums, so there is plenty to search for.

Once you have a singleton class/script, you can create static var’s that are available all throughout your game lifespan.

I use var’s like demo and pause etc to allow every script in every level to know if my game is in demo mode or paused etc…

that gives you globals, but no compiler level features which #ifdef etc are.

yours can only disable it instead of prevent the stuff from beeing in the app at all, which at least on the iphone is a day / night difference.

Would love to see that feature or to know how to use it if already present.

Well, that’s not really what’s being asked…just having a “isDemo” variable means that all your code is being included in all projects. With #define symbols, the demo code simply wouldn’t be included in the non-demo project at all.

However, I don’t know the actual answer, sorry. :wink:

–Eric

Ok, but mine way WILL give them the functionality to do what they specifically asked for, if there is no way to actually implement/use compiler directives.

Original OP asked for a way to have his entire game know it is in demo mode etc. Not a way to specifically exclude code at compile time.

Yes, the extra benefits would be great to have, but the OP can do what he needs with this.

If you had a global constant, and then used that in a conditional statement, would the .NET compiler not optimize the unreachable code away?

eg.

const bool alwaysTrue = true;

/* ... */

if(alwaysTrue)
  // Do this
else
  // Do that

Would the “Do that” code actaully be included in the assembly?

The question was mainly and only targeted at how to exclude code at compile time.
If you reread it you will see that he mentions a compiler flag (DEBUG), as well as a preprocessor functionality (#define), both only existant at compile time.

peter: not sure if it does. But given that C# has #ifdef and related, there is the chance it does not remove unreachable code. Instead it will likely give you a warning that you have unreachable code.
But thats something which likely only ReJ or another dev can answer

Good point…I gave that a try and the unreachable code is not, in fact, included in the build. (You don’t have to be a Unity dev to be able to see what code is included in the build. :wink: )

–Eric

If you need this feature just spend some votes here
http://feedback.unity3d.com/pages/15793-unity-iphone/suggestions/183148-conditional-compilation-support

Actually, this is a Unity feature I had been requesting for quite a while now … it’s not really related to Unity iPhone IMHO. In fact, Unity iPhone does use conditional compilation symbols … at least one: UNITY_IPHONE (if I remember correctly), which is automatically passed to the compiler when compiling with Unity iPhone, see also:

http://forum.unity3d.com/viewtopic.php?t=15650

… but what we’d need is really to be able to set those directives in the editor.

One workaround (that’s what I’m currently using) is have #define XYZ in every file that uses the relevant directive (or, simply, all class files). Then, if you do builds, go through all code-files and replace #define XYZ with //#define XYZ (in other words “comment it out”) if you want to disable it.

It’s not too hard to write a little Mono or .NET script that does this for you :wink:

Project-wide conditional compilation is possible in Visual Studio by opening the Project Properties (right-click on Project in Solution Explorer pane, select Properties), opening the “Build” tab, and entering the desired symbols in the text box labeled “Conditional compilation symbols”.

You could also try an Editor script that iterates through all of your files and adds the appropriate #define the start of each before creating the build.

Since this old thread was just resurrected, I figured I should leave this info below for anyone searching for this in the future:

There is support for global defines but it is undocumented. You just need to create a special file in your root /Assets directory.

See this thread for more info: http://forum.unity3d.com/threads/71445-How-To-Set-Project-Wide-pragma-Directives-with-JavaScript

Misc Notes:
us.rsp = UnityScript / JavaScript
gmcs.rsp / smcs.rsp = C#, which one varies depending on the .NET profile you are targeting
Any changes to these files will not kick off a recompile of your code, so you need to manually force this to happen if you want to see your changes.

We are improving the documentation on Custom Defines, and it’s important to update these threads with the correct way of using them in the current versions of Unity. If you want to modify only global defines, you should use Scripting Define Symbols in Player Settings, because this will cover all the compilers. If you choose the .rsp files instead, you’ll have to provide one file for every compiler Unity uses, and you won’t know when one or another compiler is used. To do this you must add a text file with the extra directives to the “Assets/” folder:

C# /Assets/smcs.rsp
C# - Editor Scripts /Assets/gmcs.rsp
UnityScript /Assets/us.rsp
Boo /Assets/boo.rsp

As an example, if you include the single line “-define:UNITY_DEBUG” in your smcs.rsp file the define UNITY_DEBUG will exist as a global define for C# scripts, except for Editor scripts. Every time you make changes to .rsp files you will need to recompile for them to be effective. You can do this by updating or reimporting a single script (.js, .cs or .boo) file. All this information is being added to our documentation.