You can set the screen mode from code based on whether the device is a phone/tablet or TV.
First step is to check if this app is running on a TV or mobile device.There is no official API to do this on Android but this post describes how to do that as a plugin with AndroidJavaClass
.
bool isAndroidTv()
{
#if !UNITY_ANDROID || UNITY_EDITOR
return false;
#else
AndroidJavaClass unityPlayerJavaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject androidActivity = unityPlayerJavaClass.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass contextJavaClass = new AndroidJavaClass("android.content.Context");
AndroidJavaObject modeServiceConst = contextJavaClass.GetStatic<AndroidJavaObject>("UI_MODE_SERVICE");
AndroidJavaObject uiModeManager = androidActivity.Call<AndroidJavaObject>("getSystemService", modeServiceConst);
int currentModeType = uiModeManager.Call<int>("getCurrentModeType");
AndroidJavaClass configurationAndroidClass = new AndroidJavaClass("android.content.res.Configuration");
int modeTypeTelevisionConst = configurationAndroidClass.GetStatic<int>("UI_MODE_TYPE_TELEVISION");
return (modeTypeTelevisionConst == currentModeType);
#endif
}
Then you can change the screen orientation with Screen.orientation
, in the Awake
function:
void Awake()
{
bool androidTv = isAndroidTv();
Screen.autorotateToLandscapeLeft = androidTv;
Screen.autorotateToLandscapeRight = false;
Screen.autorotateToPortrait = !androidTv;
Screen.autorotateToPortraitUpsideDown = false;
if (androidTv)
{
Screen.orientation = ScreenOrientation.LandscapeLeft;
}
else
{
Screen.orientation = ScreenOrientation.Portrait;
}
}
You can use this in a "game controller" object that is set up to run early through the script order settings.
Also, you'll want to set the Android player settings to have the Default Orientation set to Auto Rotation.