GraphicsDevice 视口在游戏启动时和从后台恢复后发生变化 [Android 上的 Monogame]

GraphicsDevice Viewport changes when Game is started and after resume from background [Monogame on Android ]

我研究这个问题已经有一段时间了,但一直想不通。在某些 Android 设备上,当游戏开始时,我的游戏会在右侧出现一个 "dead" space 的垂直条,如果我将游戏发送到后台并重新激活它,我就会得到右边还有一个。我在 post 中添加了两个 GIFS 来帮助展示这一点。

当我检查 GraphicsDevice.Viewport.X 它不等于零时,我可以将其设置回零,但如果我将游戏置于后台并重新启动,X 值又会发生变化。我可以使用下面的代码来纠正它,但很难找到我应该把它放在哪里以避免每次更新都需要调用它。同样,视口的宽度也会同时改变。

if (GraphicsDevice != null && (GraphicsDevice.Viewport.X != 0 ||
    GraphicsDevice.Viewport.Width != GraphicsDevice.Adapter.CurrentDisplayMode.Width ||
    GraphicsDevice.Viewport.Width != GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width))
  {
    Viewport view = GraphicsDevice.Viewport;
    view.X = 0;
    view.Width = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
    GraphicsDevice.Viewport = view;
  }

我已将此代码放在屏幕的 Update() 方法中,它解决了问题 - 但在每次更新时调用它似乎有些过分。我还尝试在 Activity 和 Game OnResume() 和 OnActivate() 的覆盖中调用它,它会根据需要更改视口,但不会保持并最终设置回导致酒吧 'instantly'.

另外值得注意的是,游戏 运行 处于全屏模式。

如果您能提供任何建议,我们将不胜感激。

Game Started Game resumed from background

注意:在视频中我是通过触摸移动蓝色标记。

为了完整起见,我在这里发布了我找到的问题解决方案。在 Monogame Community 上寻求帮助后找到了解决方案。我从主 Activity.

中的 OnWindowFocusChanged() 事件调用了以下方法
private void HideSystemUI()
{
// Apparently for Android OS Kitkat and higher, you can set a full screen mode. Why this isn't on by default, or some kind
// of simple switch, is beyond me.
// Got this from the following forum post: http://community.monogame.net/t/blocking-the-menu-bar-from-appearing/1021/2
    if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
    {
        View decorView = Window.DecorView;
        var uiOptions = (int)decorView.SystemUiVisibility;
        var newUiOptions = (int)uiOptions;

        newUiOptions |= (int)SystemUiFlags.LowProfile;
        newUiOptions |= (int)SystemUiFlags.Fullscreen;
        newUiOptions |= (int)SystemUiFlags.HideNavigation;
        newUiOptions |= (int)SystemUiFlags.ImmersiveSticky;

        decorView.SystemUiVisibility = (StatusBarVisibility)newUiOptions;

        this.Immersive = true;
     }
}