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
11
Question by Elipsis · Aug 21, 2012 at 03:14 PM · androidiosinputdevice

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?

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 cregox · Nov 15, 2014 at 10:50 AM 0
Share

somewhat related: http://answers.unity3d.com/questions/552549/check-is-iphone-or-ipad.html

3 Replies

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

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");
 }
Comment
Add comment · Show 3 · 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 illustir · Apr 21, 2015 at 02:56 PM 0
Share

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

avatar image ow3n · Aug 11, 2017 at 06:48 PM 0
Share

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.

avatar image fafase ow3n · Aug 13, 2017 at 08:23 PM 1
Share

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;
  }
avatar image
36

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

Comment
Add comment · Show 7 · 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 Jix · Mar 11, 2014 at 09:43 AM 0
Share

@$$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
avatar image fafase · Apr 22, 2015 at 01:54 PM 1
Share

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

avatar image Fattie fafase · Sep 02, 2016 at 09:30 AM 0
Share

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.

avatar image $$anonymous$$ · Feb 25, 2016 at 02:02 PM 1
Share

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?

avatar image tanoshimi $$anonymous$$ · Feb 25, 2016 at 07:22 PM 2
Share

You shouldn't really ask additional questions in comments but yes, it's possible using standard condition syntax:

 #if (UNITY_IPHONE || UNITY_ANDROID)
avatar image cmberryau · Apr 01, 2016 at 04:45 AM 0
Share

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.

avatar image Jix cmberryau · Apr 02, 2016 at 12:49 AM 0
Share

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
avatar image
2

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

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

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

14 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

Related Questions

How to get sensor(e.g. accelerometer) input from external device(phone and similar) to PC Unity editor? 1 Answer

How to detect if the device is android mobile or tablet 2 Answers

Mobile device screen sizes 4 Answers

upload image to server 0 Answers

Vertical mobile input 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