Unity 如何在 Android 设备上禁用全屏 (S8) [英] Unity how to disable Fullscreen on Android devices (S8)

查看:98
本文介绍了Unity 如何在 Android 设备上禁用全屏 (S8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我构建我的应用程序并在我的三星 Galaxy S8 上播放它,它始终处于全屏模式.我什至无法在设置"中禁用它.

If I build my App and play it on my Samsung Galaxy S8 its always on Fullscreen Mode. I can´t even disable it vis Settings.

我用代码Screen.fullScreen = false;"试过了但它不起作用.在我的播放器设置中,纵横比为自定义 (16.9).

I tried it with the Code "Screen.fullScreen = false;" but it don´t work. In my playersettings the Aspect Ratio is at Custom (16.9).

你知道如何解决这个问题吗?我不希望它全屏显示.

Do you have any Idea how to solve this Problem? I want it not to be on fullscreen.

推荐答案

这在过去很容易通过创建新的 UnityPlayer 子类并将 setFullscreen(true); 修改为 setFullscreen(false); 但是 Unity API 和 Android 版本已经发生了很大的变化,这不再那么容易了.

This was easy to do in the past by creating new UnityPlayer subclass and modifying the setFullscreen(true); to setFullscreen(false); but Unity API and Android version has changed a lot and this can't be done anymore that easy anymore.

您必须从 C# com.unity3d.player.UnityPlayer 获取当前的 Unity 活动,从中获取 Android 的 WindowManager 然后清除 FLAG_FULLSCREEN旗帜.

You have to get current Unity activity from C# com.unity3d.player.UnityPlayer, get Android's WindowManager from it then clear the FLAG_FULLSCREEN flag.

这个人已经做到了下面是他/她的代码的样子:

This person has already done it and below is what his/her code looks like:

public static void SetupAndroidTheme(int primaryARGB, int darkARGB, string label = null)
{
    #if UNITY_ANDROID && !UNITY_EDITOR
    label = label ?? Application.productName;
    Screen.fullScreen = false;
    AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
    activity.Call("runOnUiThread", new AndroidJavaRunnable(() =>
    {
        AndroidJavaClass layoutParamsClass = new AndroidJavaClass("android.view.WindowManager$LayoutParams");
        int flagFullscreen = layoutParamsClass.GetStatic<int>("FLAG_FULLSCREEN");
        int flagNotFullscreen = layoutParamsClass.GetStatic<int>("FLAG_FORCE_NOT_FULLSCREEN");
        int flagDrawsSystemBarBackgrounds = layoutParamsClass.GetStatic<int>("FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
        AndroidJavaObject windowObject = activity.Call<AndroidJavaObject>("getWindow");
        windowObject.Call("clearFlags", flagFullscreen);
        windowObject.Call("addFlags", flagNotFullscreen);
        windowObject.Call("addFlags", flagDrawsSystemBarBackgrounds);
        int sdkInt = new AndroidJavaClass("android.os.Build$VERSION").GetStatic<int>("SDK_INT");
        int lollipop = 21;
        if (sdkInt > lollipop)
        {
            windowObject.Call("setStatusBarColor", darkARGB);
            string myName = activity.Call<string>("getPackageName");
            AndroidJavaObject packageManager = activity.Call<AndroidJavaObject>("getPackageManager");
            AndroidJavaObject drawable = packageManager.Call<AndroidJavaObject>("getApplicationIcon", myName);
            AndroidJavaObject taskDescription = new AndroidJavaObject("android.app.ActivityManager$TaskDescription", label, drawable.Call<AndroidJavaObject>("getBitmap"), primaryARGB);
            activity.Call("setTaskDescription", taskDescription);
        }
    }));
    #endif
}

public static int ToARGB(Color color)
{
    Color32 c = (Color32)color;
    byte[] b = new byte[] { c.b, c.g, c.r, c.a };
    return System.BitConverter.ToInt32(b, 0);
}

示例调用:

SetupAndroidTheme(ToARGB(Color.black), ToARGB(Color.black));

未在三星 Galaxy S8 上进行测试,但它适用于我的 Android 设备.

Not tested on Samsung Galaxy S8 but it works on my Android device.

这篇关于Unity 如何在 Android 设备上禁用全屏 (S8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆