Unity Game Development: Detect Android TV at Runtime

I am a Developer Advocate for Strategic Games partnerships at Google. My role allows me to work with a lot of talented people in the games industry. The thing I enjoy most about my job is that I get to help developers solve interesting engineering problems which provides a tone of learning material. This post is my first on Medium.com and I will provide some code to solve a problem that I have answered a number of times for Unity game developers.

Problem:

“How can I detect if my game is running on Android TV at run time?”

Answer:

(first part is how to do it using the Android APIs then I give a code example of how to accomplish the same thing in Unity/C#)

The Android Way:
(see: https://developer.android.com/training/tv/start/hardware.html)

public static final String TAG = "DeviceTypeRuntimeCheck";

UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
Log.d(TAG, "Running on a TV Device")
} else {
Log.d(TAG, "Running on a non-TV Device")
}

The code listed above is simple enough to understand for someone who spends their time writing Android Apps regularly. However it’s not as easy to translate it to your code base if you are used to being one level removed from Android, such as when you are developing a cross platform game in Unity.

The Android Way in Unity:

The great news is that its fairly easy to run the code listed above from within Unity. You just need to use the Unity interface to Android APIs.

I packaged up the example project as a Unity package which you can find here: https://goo.gl/N8HAfn

The code is documented and if you run it on an Android TV you will see:

And if you run it on an Android device that is not a TV you will see:

I hope this helps you with your game development. I always enjoy seeing new games on Android TV. Let me know your thoughts, comments and or what you are working on.

Have a great day