Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by bruce965 · Jun 13, 2013 at 04:00 PM · c#client-serverdefinedependent

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamora · Jun 13, 2013 at 04:37 PM 0
Share

Have you looked at Unity's Application.isEditor ? Would that work ins$$anonymous$$d of using preprocessor directives?

avatar image bruce965 · Jun 13, 2013 at 04:42 PM 0
Share

@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.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bruce965 · Nov 03, 2013 at 08:42 AM 0
Share

This is the simplest yet better solution! I didn't think about it.

avatar image Datael · Nov 11, 2013 at 02:03 AM 0
Share

This works better than my solution for new projects, also. +1

avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bruce965 · Jun 13, 2013 at 04:52 PM 1
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to exclude code if a certain script is not present in the project. 2 Answers

ClientRpc Function not being carried out on all clients in Unity3d c# 0 Answers

Initialising List array for use in a custom Editor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges