Portrait mode for Phones/Tablets and Landscape mode for Android TV?
My app has been designed to use portrait mode in phones and tablets. I'm trying to support Android TV which requires landscape mode, so my app is now using different layouts based on whether the device is Android TV or not.
The problem is that Google rejects apps from Android TV that use the "portrait" feature. How can I keep "portrait" for phones/tablets and switch to landscape only for TV with the same APK? Is the only way to do this to make two different APKs? I'd like to avoid this if possible.
PS. I'm using Unity 3D which might limit some of the more obscure solutions.
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.
链接地址: http://www.djcxy.com/p/37962.html上一篇: Android平板电脑风景和肖像模式