Detect if compiling from Unity project

I’m making a client/server game, where the client is built from inside Unity, and the server is standalone.

Some classes are shared between the two, but constructors shouldn’t be compiled in client version.

I have decided to define if the project is being built from Unity or from MonoDevelop by using preprocessor directive “DEFINE”.

I know
Platform Dependent Compilation
but it doesn’t appear to provide any define that is included in every Unity version and any platform, I’d prefer to not insert too many OR operators.

So the question is: is there any (undocumented) define suitable for this?

Thank you!

I realize that this has already been answered but trying to figure out how to do this myself I came across your question, and I have since found a slightly different way to do it which is closer to what you originally wanted to do:

You can set compiler directives in Player Settings. If you add UNITY to each of the compile targets’ individual Player Settings pages under Scripting Define Symbols (for good reason this value isn’t shared) you can then just use this pattern in your code:

    // Unity-only code

Or in my case I used #if !UNITY as I wanted to remove multiple Console calls.

You will still have to define this on all of the Player Settings per build target, but once it’s done, you can forget about it.

For anyone who is looking for a way without having to add ORs everywhere, this should do the trick. The only downside is that you have to write it on every page of Player Settings, but it will keep the defines separate from your code and isolated to Unity, so it worked well when applied to a library in my case.

In your server-side project codes, you can set C# defines in MonoDevelop or Visual Studio.

e.g., Set compilation define SERVER and then in your codes you can do:

'#ifdef SERVER

// server side codes


can you (and i recall having issues with this but i may have just mistyped it):

#if (unity_2 || unity_3 || unity_4)
  #define is_from_unity

#if is_from_unity
  // u nity-specific stuff here