Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Android TV detection

Discussion in 'Android' started by Radiangames2, Feb 5, 2015.

  1. Radiangames2

    Radiangames2

    Joined:
    Aug 22, 2012
    Posts:
    31
    Is there any way to detect when the game is being played on an Android TV? Google has requested changes to my game (not linking to games not compatible with Android TV, for example), and I can't figure out any way to tell if the game is running on Android TV or not.

    Any help/plug-in suggestion would be greatly appreciated.

    Thanks!
    Luke
     
  2. njwm

    njwm

    Joined:
    Apr 30, 2013
    Posts:
    26
    I have the same problem, anyone have a solution?
     
  3. BlastoIO

    BlastoIO

    Joined:
    Mar 3, 2015
    Posts:
    5
    List out the points that they outlined you on.

    When we originally put up our APK (Unity 4.6), we got hit with:

    Please refer to our Home Screen Banner and UI Patterns documentation:
    http://developer.android.com/training/tv/start/start.html#banner
    http://developer.android.com/design/tv/patterns.html#banner

    Your game must have the "isGame" flag in the AndroidManifest.xml set to
    true. Android TV uses the android:isGame flag to differentiate games with
    apps.

    Please refer to our Manifest documentation for details:
    http://developer.android.com/training/tv/games/index.html#manifest

    Your app or game that does require a game controller, is inaccessible with
    a standard Android game controller.

    Please refer to our Game Controller Support documentation for details:
    http://developer.android.com/training/tv/games/index.html#gamepad



    Banner issue was fixed by adding and resizing our banner to a higher DPI since Android TV is not DPI specific. We took the (Unity 5) suggested size and increased by 33%.

    Our fixes were to the rest were to the following in AndroidManifest:

    - Add after screen list:
    <uses-feature android:name="android.hardware.gamepad" android:required="false"/>

    - Add into <application> tag:
    android:isGame="true"

    - (Unity 4.6) Add into <application tag> to add a banner, make sure you throw a banner.png into res/drawables as well:
    android:banner="@drawable/banner"

    - Add after category for android.intent.category.LAUNCHER:
    <category android:name="android.intent.category.LEANBACK_LAUNCHER" />


    We're about to push our first Unity 5 build within the next few hours, so we'll see how this goes with the new changes that Unity has done.
     
  4. florianpenzkofer

    florianpenzkofer

    Unity Technologies

    Joined:
    Sep 2, 2014
    Posts:
    180
    I have not tried it, but check the sample code in the "Check for a TV Device" section on https://developer.android.com/training/tv/start/hardware.html.
     
  5. Cenda

    Cenda

    Joined:
    Jun 3, 2010
    Posts:
    50
    How can I use native code to Unity? Is there some plugins. It would be great if I can test if it is Android TV. Now I have to publish different APK.
     
  6. trawitz

    trawitz

    Joined:
    Oct 6, 2014
    Posts:
    1
    I was able to detect it by doing the following. This basically calls all that java code from that link in C#. It's a bit cryptic, but I needed a quick solution.

    Code (CSharp):
    1. // Essentially this code is doing some java stuff to detect if the UI is in TV mode or not
    2.         // What it does is it gets the Android Activity that is running Unity,
    3.         // gets the value of android.content.Context.UI_MODE_SERVICE so we can call getSystemService on
    4.         // the activity, passing in the UI_MODE_SERVICE, which gets us the UiModeManager.  Next we
    5.         // call getCurrentModeType on our UiModeManager instance which gives us some integer that represents the UI mode.
    6.         // We then have to get the value of android.content.res.Configuration.UI_MODE_TYPE_TELEVISION as an integer and then
    7.         // finally we can compare that with our mode type and if they match, it is android tv.
    8.         AndroidJavaClass unityPlayerJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    9.         AndroidJavaObject androidActivity = unityPlayerJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
    10.         AndroidJavaClass contextJavaClass = new AndroidJavaClass("android.content.Context");
    11.         AndroidJavaObject modeServiceConst = contextJavaClass.GetStatic<AndroidJavaObject>("UI_MODE_SERVICE");
    12.         AndroidJavaObject uiModeManager = androidActivity.Call<AndroidJavaObject>("getSystemService", modeServiceConst);
    13.         int currentModeType = uiModeManager.Call<int>("getCurrentModeType");
    14.         AndroidJavaClass configurationAndroidClass = new AndroidJavaClass("android.content.res.Configuration");
    15.         int modeTypeTelevisionConst = configurationAndroidClass.GetStatic<int>("UI_MODE_TYPE_TELEVISION");
    16.  
    17.         if(modeTypeTelevisionConst == currentModeType)
    18.         {
    19.             Debug.Log ("This is an AndroidTV device");
    20.         }
    21.         else
    22.         {
    23.             Debug.Log ("This is NOT an AndroidTV device");
    24.         }
     
    JJC1138 likes this.
  7. dubayou

    dubayou

    Joined:
    Jan 23, 2015
    Posts:
    2
    @trawitz thanks!

    Just a note, your gonna wanna wrap this with build defs like so



    Code (CSharp):
    1.     bool isAndroidTv()
    2.     {
    3.         #if ! UNITY_ANDROID || UNITY_EDITOR
    4.             return false;
    5.         #else
    6.  
    7.         // Essentially this code is doing some java stuff to detect if the UI is in TV mode or not
    8.         // What it does is it gets the Android Activity that is running Unity,
    9.         // gets the value of android.content.Context.UI_MODE_SERVICE so we can call getSystemService on
    10.         // the activity, passing in the UI_MODE_SERVICE, which gets us the UiModeManager.  Next we
    11.         // call getCurrentModeType on our UiModeManager instance which gives us some integer that represents the UI mode.
    12.         // We then have to get the value of android.content.res.Configuration.UI_MODE_TYPE_TELEVISION as an integer and then
    13.         // finally we can compare that with our mode type and if they match, it is android tv.
    14.         AndroidJavaClass unityPlayerJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    15.         AndroidJavaObject androidActivity = unityPlayerJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
    16.         AndroidJavaClass contextJavaClass = new AndroidJavaClass("android.content.Context");
    17.         AndroidJavaObject modeServiceConst = contextJavaClass.GetStatic<AndroidJavaObject>("UI_MODE_SERVICE");
    18.         AndroidJavaObject uiModeManager = androidActivity.Call<AndroidJavaObject>("getSystemService", modeServiceConst);
    19.         int currentModeType = uiModeManager.Call<int>("getCurrentModeType");
    20.         AndroidJavaClass configurationAndroidClass = new AndroidJavaClass("android.content.res.Configuration");
    21.         int modeTypeTelevisionConst = configurationAndroidClass.GetStatic<int>("UI_MODE_TYPE_TELEVISION");
    22.  
    23.         return (modeTypeTelevisionConst == currentModeType);
    24. #endif
    25.     }
    26.  
     
    Last edited: Jan 24, 2016
    MrPacoGP likes this.
  8. MrPacoGP

    MrPacoGP

    Joined:
    Jul 18, 2013
    Posts:
    259
    On my android TV isn´t working, i get false and its a android tv.