【问题标题】:Get default device orientation (Landscape or Portrait) on Android/iOS在 Android/iOS 上获取默认设备方向(横向或纵向)
【发布时间】:2014-07-07 20:06:49
【问题描述】:

我目前正在使用 Unity3D,我需要了解如何确定 Android 和 iOS 的默认设备方向(横向或纵向)。通过代码实现这一目标的最佳方法是什么(我正在使用 c#)?

感谢您的帮助!

注意:我看过以下帖子:How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout)

但我似乎无法使代码工作(也许我缺少要导入的文件或其他东西)。此外,我还需要一种方法来使其与 iOS 一起使用!

【问题讨论】:

    标签: android ios unity3d orientation screen-orientation


    【解决方案1】:

    对于 Android,您可以使用 getRotation

    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    
    switch (rotation) {
    case 0:
        return Surface.ROTATION_0;
    case 90:
        return Surface.ROTATION_90;
    case 180:
        return Surface.ROTATION_180;
    case 270:
        return Surface.ROTATION_270;`
    

    对于 iOS this 可能会有所帮助:

    UIInterfaceOrientation orientation = 
                        [UIApplication sharedApplication].statusBarOrientation;
    
    if(orientation == 0) //Default orientation
    //UI is in Default (Portrait) -- this is really a just a failsafe. 
    else if(orientation == UIInterfaceOrientationPortrait)
    //Do something if the orientation is in Portrait
    else if(orientation == UIInterfaceOrientationLandscapeLeft)
    // Do something if Left
    else if(orientation == UIInterfaceOrientationLandscapeRight)
    //Do something if right
    

    另外,这个帖子也有类似的答案:Determining the Orientation of the screen rather than the orientation of the device

    【讨论】:

    • 我应该补充一点,如果您已明确将活动的方向设置为横向/纵向,则“getRotation()”中的值不会考虑到这一点并返回错误的“自然”方向(这将是您所要求的方向)。
    • 你的rotation 是常量 0、1、2 或 3,即 Surface.ROTATION_0、90、180、270。因此需要编辑你的 switch 语句。见:developer.android.com/reference/android/view/…
    【解决方案2】:

    居然自己找到了自己需要的东西!

    这里是链接,任何感兴趣的人:https://gist.github.com/flarb/3252857

    (最后我不需要iOS检查,因为我们的应用程序只支持平板电脑,而且我认为所有iOS设备上的默认方向都是纵向(可能是错误的))

    干杯!

    【讨论】:

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