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.