Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CookieSteve · Mar 22, 2017 at 09:32 AM · windowsif-statementsplatformosxdependencies

Why is Unity identifying my computer as both a Windows and a Mac?

I'm writing code on Windows that needs to be compatible with both Windows and OSX computers. I know that to write platform-dependent code you have to use #if statements, but I'm having trouble with it.

Here's an example of the kind of code I've been writing:

 #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
     public bool isWindows = true;
 #else
     public bool isWindows = false;
 #endif
 
 #if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
     public bool isOSX = true;
 #else
     public bool isOSX = false;
 #endif

I know this looks redundant, but I need it to prove a point. If I use Debug.Log to print out the value of these booleans, I get this:

 isWindows: TRUE
 isOSX: TRUE

I don't understand why, but Unity seems to think my computer is both a Windows and a Mac. I'm testing this in the Windows editor, but it isn't exclusive to that. When I was testing it on the Mac standalone version, it was recognising the computer as a Windows there too.

Can anyone explain why this might be happening?

Comment
Add comment · Show 1
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 hexagonius · Mar 22, 2017 at 09:42 AM 0
Share

sounds like a bug to me. according to the docs these directives should be exclusive to their respective platforms. I'd report it as a bug.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Mar 22, 2017 at 09:48 AM

Are those public fields in a serialized class? That means once the value has been set it doesn't matter what you set in the field initializer since the value is deserialized before Awake. Make sure you don't serialize the values. Either by using the [NonSerialized] attribute or simply by making it static. "Static" or "const" would make the most sense here since those are in fact constants.

Unity doesn't "think" ^^. The preprocessor tags are simply set automatically depending on the platform. And no, it will never set windows and mac tags at the same time.

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 CookieSteve · Mar 22, 2017 at 09:52 AM 0
Share

What do you mean by serialized? That's probably the problem, but I'd like to know what it actually means to avoid making the same mistake in the future.

And just to make sure, is making those variables constants as simple as changing the line to 'public const bool isWindows = true;'?

avatar image Bunny83 CookieSteve · Mar 22, 2017 at 10:37 AM 0
Share

Yes, that would make them constants.

If you want to read more about serialization in Unity, have a look at the "Serialization" section in the manual.

Just in case you haven't seen this yet, the Application class has a static field called platform. It's usually the prefered way of checking the platform at runtime. It returns an enum of type RuntimePlatform.

Preprocessor directives are ment to exchange platform specific code. It's barely used to set platform specific "values". If you want to distinguish between windows and mac in a runtime script, it's enough to use UNITY_STANDALONE_OSX and UNITY_STANDALONE_WIN. Those are also set when executing inside the editor.

Note that "UNITY_STANDALONE_OSX" and "UNITY_STANDALONE_WIN" soley depend on the target platform that you selected in the build settings while "UNITY_EDITOR_WIN" and "UNITY_EDITOR_OSX" actually identify the platform the editor is running on.

So your code can still return true for both in case you have selected "Windows" as target platform but your editor runs on a $$anonymous$$ac. In this case "UNITY_STANDALONE_WIN" is set because that's your build target and "UNITY_EDITOR_OSX" is set because that's your editor.

The same is true for the reverse case when you work on a windows machine and have set the build target to OSX. In that case "UNITY_EDITOR_WIN" and "UNITY_STANDALONE_OSX" is set.

The UNITY_EDITOR_XXX directives are ment for editor scripts. If you actually build your game the editor tags won't be set.

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

69 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 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 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 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

VCRuntime errors with Universal Windows Platform, but it runs within Unity 0 Answers

Are there statistics of who develops on OSX with unity vs. Windows? 1 Answer

OSX written apps can run in Win? 1 Answer

Kinect doesn't work on the virtual machine or Mac. 0 Answers

TextMeshPro - Input field - UWP - Input field is not working correctly in uwp build, also enter works twice 0 Answers


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