Preprocessor like #if UNITY_ANDROID would run on android emulator?

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:

#if  UNITY_IPHONE
    Debug.Log("Running on Apple Device.");
#elif UNITY_ANDROID
    Debug.Log("Running on Android Device.");
#elif UNITY_WP8
    Debug.Log("Running on WP Device.");
#else
    Debug.Log("Not running on mobile Device.");
#endif

Thanks in advance

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.

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.

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.

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?

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!

A little off topic, but just an FYI:

#if UNITY_ANDROID
  //code here
#endif

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:

#if UNITY_ANDROID && !UNITY_EDITOR
//code here
#endif

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/2799097/how-can-i-detect-when-an-android-application-is-running-in-the-emulator

2 Likes

Got me started already. Thanks!