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.
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.
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.
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. )
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:
… 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
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.
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.