【发布时间】:2014-07-07 20:06:49
【问题描述】:
我目前正在使用 Unity3D,我需要了解如何确定 Android 和 iOS 的默认设备方向(横向或纵向)。通过代码实现这一目标的最佳方法是什么(我正在使用 c#)?
感谢您的帮助!
但我似乎无法使代码工作(也许我缺少要导入的文件或其他东西)。此外,我还需要一种方法来使其与 iOS 一起使用!
【问题讨论】:
标签: android ios unity3d orientation screen-orientation
我目前正在使用 Unity3D,我需要了解如何确定 Android 和 iOS 的默认设备方向(横向或纵向)。通过代码实现这一目标的最佳方法是什么(我正在使用 c#)?
感谢您的帮助!
但我似乎无法使代码工作(也许我缺少要导入的文件或其他东西)。此外,我还需要一种方法来使其与 iOS 一起使用!
【问题讨论】:
标签: android ios unity3d orientation screen-orientation
对于 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
【讨论】:
rotation 是常量 0、1、2 或 3,即 Surface.ROTATION_0、90、180、270。因此需要编辑你的 switch 语句。见:developer.android.com/reference/android/view/…
居然自己找到了自己需要的东西!
这里是链接,任何感兴趣的人:https://gist.github.com/flarb/3252857
(最后我不需要iOS检查,因为我们的应用程序只支持平板电脑,而且我认为所有iOS设备上的默认方向都是纵向(可能是错误的))
干杯!
【讨论】: