适用于Android TV的手机/平板电脑和横向模式的纵向模式?

时间:2016-12-12 22:13:38

标签: android android-layout unity3d

我的应用程序设计为在手机和平​​板电脑中使用纵向模式。我正在尝试支持需要横向模式的Android TV,因此我的应用现在根据设备是否为Android TV使用不同的布局。

问题在于Google拒绝使用“纵向”功能的Android TV应用。如何为手机/平板电脑保留“肖像”并仅使用相同的APK切换到横向风景?这是制作两个不同APK的唯一方法吗?如果可能的话,我想避免这种情况。

PS。我正在使用Unity 3D,这可能会限制一些比较模糊的解决方案。

1 个答案:

答案 0 :(得分:1)

您可以根据设备是手机/平板电脑还是电视,从代码设置屏幕模式。

第一步是检查此应用是否在电视或移动设备上运行。在Android上没有官方API可以执行此操作,但this帖子描述了如何使用{{3}作为插件执行此操作}。

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
}

然后,您可以使用Awake函数中的AndroidJavaClass更改屏幕方向:

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;
    }
}

你可以在游戏控制器&#34;中使用它。设置为通过脚本顺序设置提前运行的对象。

此外,您还要设置Android播放器设置,将默认方向设置为自动旋转。