- Home /
How to tell if the device is android or ios
Hi guys, I got unity with both the ios and android versions and now I want to specifically do something in code for ios, and something else for android (without building 2 separate games). So like you're thinking, simple, find out if it's ios or if it's android and put an if else. And here's where I'm suck. Yeah, it's probably really obvious, do you guys know?
Answer by fafase · Aug 22, 2012 at 10:47 AM
function Start () {
if (Application.platform == RuntimePlatform.Android)
print ("Android");
else if(Application.platform == RuntimePlatform.IPhonePlayer)
print("Ifone");
}
Thanks for this! This makes iPhone code not work when playing in the editor without having to switch platforms (which is what I was looking for).
A caveat to using this method: Even though this method separates conditions under which the code will run, ALL code will be compiled regardless of platform and produce an error.
Indeed, which is why the other answer using macros has more upvotes. This is ok for basic actions like
int value;
void Start () {
if (Application.platform == RuntimePlatform.Android)
value = 5;
else if(Application.platform == RuntimePlatform.IPhonePlayer)
value = 10;
}
Answer by MadDave · Aug 22, 2012 at 10:41 AM
The cleanest way is to use the preprocessor. Basically like this:
#if UNITY_IPHONE
... iPhone code here...
#endif
#if UNITY_ANDROID
... Android code here...
#endif
NOTE IT IS ALL-CAPS "ANDROID".
THE TWO COMMENTS BELOW ARE WRONG .
Further reading: http://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html
@$$anonymous$$adDave will this be working while declaring fields or functions as well? for example:
#if UNITY_IPHONE
//some fields and functions used for IOS only
#endif
#if UNITY_Android
//somew fields and functions used for Android only
#endif
This will be working and has one advantage over my answer is that whatever is inside the statement will only be built for that platform. For instance in our app, we have sharing for iOS but not for android, so all the sharing goes within the proper macros and will not exist in Android.
Note that it needs to be spelt properly:
#if UNITY_ANDROID // Cap letters
#elif UNITY_IPHONE
#else
#endif
This answer is simply wrong. Fafase's answer is correct.
Note that the OP asks
"without building 2 separate games"
you use the approach in this answer IF you are making two separate builds. Utterly different, do what Fafase says in the correct answer if you want to do what the OP asks.
Is there a way to check for multiple platforms in the same preprocessor-if statement?
#if UNITY_IPHONE || UNITY_ANDROID
Debug.Log("mobile");
#endif
Or do I have to do it like this?
#if UNITY_IPHONE
#define $$anonymous$$OBILE
#endif
#if UNITY_ANDROID
#define $$anonymous$$OBILE
#endif
#if $$anonymous$$OBILE
Debug.Log("mobile");
#endif
Is this even possible?
You shouldn't really ask additional questions in comments but yes, it's possible using standard condition syntax:
#if (UNITY_IPHONE || UNITY_ANDROID)
This answer does not deter$$anonymous$$e if the device is Android or iOS, it only deter$$anonymous$$es if you're compiling with certain flags.
This solution completely fails when you are compiling external Assemblies as the flags are resolved and compilation is decided upon at compile time and cannot therefore deter$$anonymous$$e the device platform at runtime.
Actually it does! When compiling for iOS it will NOT compile the code that's meant for Android only. And vise versa.
You want to send an API call to store if the device is iOS? easy!
#if UNITY_IPHONE
...send API call with iOS tag...
#endif
#if UNITY_ANDROID
... send API call with iOS tag...
#endif
Answer by ErbombaLP · Feb 15, 2017 at 10:56 AM
Nowadays UNITY_IPHONE is deprecated, better to use UNITY_IOS:
#if UNITY_IOS
...IOS specific
#endif
https://docs.unity3d.com/Manual/PlatformDependentCompilation.html