简体   繁体   English

获取Android / iOS上的默认设备方向(横向或纵向)

[英]Get default device orientation (Landscape or Portrait) on Android/iOS

I am currently working with Unity3D and I need to find out how to determine default device orientation (landscape or portrait) for both Android and iOS. 我目前正在使用Unity3D,我需要了解如何确定Android和iOS的默认设备方向(横向或纵向)。 What is the best way to achieve this through code (I'm working with c#) ? 通过代码实现此目标的最佳方法是什么(我正在使用c#)?

Thanks for the help ! 谢谢您的帮助 !

Note : I've seen the following post : How to check device natural (default) orientation on Android (ie get landscape for eg, Motorola Charm or Flipout) 注意:我看过以下文章: 如何在Android上检查设备自然(默认)方向(即获取风景,例如Motorola Charm或Flipout)

But I can't seem to make the code work (maybe I'm missing a file to import or something). 但是我似乎无法使代码正常工作(也许我缺少要导入的文件或其他东西)。 Besides, I need a way to make this work with iOS as well ! 此外,我还需要一种使它在iOS上也能使用的方法!

For Android you could use getRotation : 对于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;`

For iOS this might help : 对于iOS, 可能会有所帮助:

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

Also, this thread has similar answer : Determining the Orientation of the screen rather than the orientation of the device 另外,该线程具有类似的答案: 确定屏幕的方向而不是设备的方向

Actually found what I needed by myself ! 居然找到了我自己需要的东西!

Here's the link, for anyone interested : https://gist.github.com/flarb/3252857 这是任何有兴趣的人的链接: https : //gist.github.com/flarb/3252857

(I didn't need the iOS check in the end, since our application is only supported on tablets anyway, plus I think the default orientation on all iOS devices is Portrait (might be wrong)) (最后我不需要iOS检查,因为无论如何,我们的应用程序仅在平板电脑上受支持,而且我认为所有iOS设备上的默认方向都是纵向(可能是错误的))

Cheers ! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android设备定位是风景,但传感器的行为就像定位是肖像 - Android Device Orientation is Landscape but Sensors Behaves Like the Orientation is Portrait 如何检测Android设备是纵向还是横向锁定 - How to detect whether Android device is locked in portrait or landscape orientation Android-从纵向/横向切换方向会将属性重置为默认设置 - Android - Switching orientation from portrait/landscape resets attributes to default settings 如何在 Android Print Framework 中将默认的“纵向”更改为横向? - How to change the default “Portrait Orientation” to Landscape in Android Print Framework? 在纵向方向机器人的风景布局 - Landscape layout in portrait orientation android 相机预览方向为横向,但设备为纵向 - Camera preview orientation is in landscape but device is in portrait Android-以横向方向强制肖像视图 - Android - Force portrait view in landscape orientation Android小部件不同的纵向和横向方向 - Android widget different portrait and landscape orientation Android屏幕方向:横向回到纵向 - Android Screen Orientation: Landscape Back to Portrait Android Camera横向至纵向方向问题 - Android Camera Landscape to Portrait orientation issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM