I’m having an issue. I’m assuming the #if UNITY_PLATFORM commands are for only running code on specific code. So I’m trying to check whether the current environment is in the Editor or running on an android device, and run different code for each platform. However using #if UNITY_EDITOR and #if UNITY_ANDROID when the build target for the editor is android, both of these run when the play button is pressed, instead of just the UNIT_EDITOR one. Can someone explain why?
These are compile-time directives.
The lines of code inside #if UNITY_ANDROID will be compiled if Platform is set to “Android” inside “Build Settings” dialog and you:
- click “Build” or
- click “Play” inside the Unity editor (and of course when the editor performs auto-build)
The lines of code code inside #if UNITY_EDITOR will be compiled no matter what Platform is set but only if you:
- click “Play” inside the Unity editor (and of course when the editor performs auto-build)
When you build your game to an Android device, lines of code wrapped with #if UNITY_ANDROID will end up “inside” the .apk file, those wrapped with #if UNITY_EDITOR won’t. #UNITY_EDITOR seems like it but is not another platform directive. Platform directives should be mutually exclusive and set to true if particular platform is built to.
If you need checking at runtime, maybe this is for you:
http://docs.unity3d.com/Documentation/ScriptReference/Application-platform.html
i know it’s a bit late but for future researches, this will execute code only on android device:
- #if UNITY_ANDROID && !UNITY_EDITOR
- //code here
- #endif
Already knew that when I wrote this post 3 years ago.
This helped me understand how these defines work.
Thank You
thank you for this great informations all of you
thank you. still useful in even 2022
true
you could use
#if UNITY_EDITOR
.//editor code
#elif UNITY_ANDROID
//platform code
#endif
which will not proceed if the first check is true, has to be in that order
however what id really like to knw is the differnce btwn
PLATFORM_ANDROID and UNITY_ANDROID