Unity如何在Android设备上禁用全屏(S8)

时间:2017-11-02 10:04:56

标签: c# unity3d

如果我构建我的应用程序并在我的三星Galaxy S8上播放它总是在全屏模式下。我甚至无法在设置中禁用它。

我尝试使用代码“Screen.fullScreen = false;”但它不起作用。 在我的播放器中,宽高比处于自定义(16.9)。

你有什么想法如何解决这个问题? 我希望它不要全屏。

1 个答案:

答案 0 :(得分:1)

过去这很容易通过创建新的UnityPlayer子类并将setFullscreen(true);修改为setFullscreen(false);来实现,但Unity API和Android版本已经发生了很大变化,这已经无法完成了那很容易。

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

This此人已完成此操作,以下是他/她的代码:

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设备。