- Home /
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?
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.
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.
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;'?
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.