Detecting Android Tablets and Phones in Unity3D

There’s been a few cases when I’ve needed to know whether one of my apps is running on an Android phone or a tablet. With Camera Birds’ gyro virtual camera, I encountered the fact that orientations are flipped differently on Android tablets and phones. By default, an Android tablet’s “natural” orientation is landscape, while a phone is portrait. This means that a 90 degree rotation is landscape on a phone, while on a tablet this becomes portrait. Get it? Neither do I. It’s another supremely awful decision that is simply par for the course with Android.

I ended up adapting a method from a Stack Overflow post used to determine natural orientation. I wrote it with Unity3D’s ability to access Android’s Java classes via the AndroidJavaClass object. This is a great feature of Unity3D that allows you to access the Android API through JNI without having to write a native plug-in.

The code is here. With this, you can tell if you are running on a tablet or a phone by checking for the natural orientation: landscape on a tablet, portrait on a phone. Even if you don’t need to flip gyro rotations, you might want to do this to separate tablet ad units from phone ads, for instance.

One thought on “Detecting Android Tablets and Phones in Unity3D

  1. That code is excellent but it show me an error:

    JNI: Unable to find method id for ‘getResources’
    UnityEngine.AndroidJavaObject:Call(String, Object[])
    NaturalOrientation:GetDeviceDefaultOrientation() (at Assets/Scripts/Common/NaturalOrientation.cs:39)
    NaturalOrientation:Start() (at Assets/Scripts/Common/NaturalOrientation.cs:27)

    Can you help me with this? Your script will be very util for me.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s