【问题标题】:change orientation in android在android中改变方向
【发布时间】:2016-09-23 12:05:03
【问题描述】:

我最初将 Orientation 设置为 AutoRotation,允许 LandscapeRight 和 LandscapeLeft。

Screen.orientation = ScreenOrientation.Portrait

Screen.orientation = ScreenOrientation.AutoRotation

当我将 Screen.orientation 更改为 Portrait 并恢复为 Autorotation 时,屏幕会保持纵向,直到我旋转手机。

这个问题只发生在安卓设备上。

【问题讨论】:

    标签: android unity3d


    【解决方案1】:

    你不认为这是自然行为吗?您将方向设置为纵向,手机转到纵向,然后您更改为自动旋转,现在这将根据您持有设备的方式改变方向。它不会强制应用程序转到允许的方向进行自动旋转。除非您旋转它,否则它将保持在那里。如果要立即更改方向,则需要将方向显式设置为特定值。 Read More

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      这是你应该做的:

      1.更改为纵向。

      2。当您决定改回自动旋转时,请执行此操作。

      3.读取当前方向(DeviceOrientation)。将其转换为ScreenOrientation,然后将其分配给屏幕方向。

      注意:您不应该使用Screen.orientation 读取当前屏幕方向。这是通过Input.deviceOrientation 完成的。

      这三个步骤放入代码:

      Screen.orientation = ScreenOrientation.Portrait;
      Screen.orientation = ScreenOrientation.AutoRotation;
      Screen.orientation = DeviceToScreenOrientation(Input.deviceOrientation);
      

      DeviceToScreenOrientation 函数将Input.deviceOrientation/DeviceOrientation 枚举转换为Screen.orientation/ScreenOrientation 枚举。

      ScreenOrientation DeviceToScreenOrientation(DeviceOrientation dvceOr)
      {
          ScreenOrientation scrOr = ScreenOrientation.Unknown;
          switch (dvceOr)
          {
              case DeviceOrientation.Unknown:
                  scrOr = ScreenOrientation.Unknown;
                  break;
      
              case DeviceOrientation.Portrait:
                  scrOr = ScreenOrientation.Portrait;
                  break;
      
              case DeviceOrientation.PortraitUpsideDown:
                  scrOr = ScreenOrientation.PortraitUpsideDown;
                  break;
      
              case DeviceOrientation.LandscapeLeft:
                  scrOr = ScreenOrientation.LandscapeLeft;
                  break;
      
              case DeviceOrientation.LandscapeRight:
                  scrOr = ScreenOrientation.LandscapeRight;
                  break;
      
              //Don't know What Enum to use. Implement to match your need
              case DeviceOrientation.FaceUp:
                  //scrOr = ?
                  break;
      
              case DeviceOrientation.FaceDown:
                  //scrOr = ?
                  break;
          }
          return scrOr;
      }
      

      您可能需要切换第 2 步和第 3 步

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-05
        • 1970-01-01
        相关资源
        最近更新 更多