- Home /
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!
Have you looked at Unity's Application.isEditor
? Would that work ins$$anonymous$$d of using preprocessor directives?
@Jamora The problem is that in the Server, I cannot access Unity-specific classes, and Application
is.
EDIT: and actaually, I don't just want to know if in editor... I just want to know if in Unity.
Answer by eskimojoe · Nov 03, 2013 at 02:26 AM
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
'#endif
This is the simplest yet better solution! I didn't think about it.
This works better than my solution for new projects, also. +1
Answer by Datael · Nov 02, 2013 at 04:42 AM
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:
#if UNITY
// Unity-only code
#endif
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.
Answer by Loius · Jun 13, 2013 at 04:41 PM
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
#endif
#if is_from_unity
// u nity-specific stuff here
#endif
@Louis Having too many OR operators is what I wanted to avoid (for code readability reasons) but this is probably gonna be what I'll be using....
Thank you everybody! :)
Your answer
