Search Unity

Preprocessor like #if UNITY_ANDROID would run on android emulator?

Discussion in 'Scripting' started by GeorgeRigato, Nov 21, 2013.

  1. GeorgeRigato

    GeorgeRigato

    Joined:
    Jul 5, 2012
    Posts:
    58
    My question is how to make sure the app is running from a mobile device and not over a emulator? Unity Preprocessor can tell the difference?

    My code is something like this:

    Code (csharp):
    1. #if  UNITY_IPHONE
    2.     Debug.Log("Running on Apple Device.");
    3. #elif UNITY_ANDROID
    4.     Debug.Log("Running on Android Device.");
    5. #elif UNITY_WP8
    6.     Debug.Log("Running on WP Device.");
    7. #else
    8.     Debug.Log("Not running on mobile Device.");
    9. #endif
    Thanks in advance
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    The preprocessors are determined at build time. So if you are building an Android APK then UNITY_ANDROID is true. Thus there should be no difference between Android device and the Android emulator.

    I'm not sure how to tell if the app is running on emulator. But preprocessors will not be the way to do it.
     
  3. Zeblote

    Zeblote

    Joined:
    Feb 8, 2013
    Posts:
    1,102
    It's not possible. The whole point of an emulator is to run the app in a virtual environment on a different operating system and have it not know about this.
     
  4. GeorgeRigato

    GeorgeRigato

    Joined:
    Jul 5, 2012
    Posts:
    58
    That makes sense. I'll keep looking. Thanks for your time!

    I agree, but could be a way to identify the emulator itself, or catch some hardware discrepancy? Thanks for your time.
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    I'd check the name or model of the device. I expect that the emulator would identify itself as such.

    What are you trying to do, though?
     
  6. GeorgeRigato

    GeorgeRigato

    Joined:
    Jul 5, 2012
    Posts:
    58
    I'll Look into that, but couldn't one change the device name on the emulator to mimic a real device?
    I'm just trying to not run the app if it is running on an emulator.
    Thanks!
     
  7. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    A little off topic, but just an FYI:

    Code (csharp):
    1.  
    2. #if UNITY_ANDROID
    3.   //code here
    4. #endif
    5.  
    This will also execute in the Editor if Android is set as your target platform. To avoid things running in the editor you would do:

    Code (csharp):
    1.  
    2. #if UNITY_ANDROID && !UNITY_EDITOR
    3. //code here
    4. #endif
    5.  
    As for emulator detection... not sure how much you'll be able to do and how much requires actual access to the SDK, but here's something to get you started:

    http://stackoverflow.com/questions/...ndroid-application-is-running-in-the-emulator
     
    Last edited: Jul 20, 2017
    vmercier and Elkis like this.
  8. GeorgeRigato

    GeorgeRigato

    Joined:
    Jul 5, 2012
    Posts:
    58
    Got me started already. Thanks!