2014-07-07 115 views
0

我目前正在使用Unity3D,我需要了解如何確定Android和iOS的默認設備方向(橫向或縱向)。通過代碼實現這一點的最佳方式是什麼(我正在使用C#)?在Android/iOS上獲取默認設備方向(橫向或縱向)

感謝您的幫助!

注:我已經看到了下面的帖子:How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or Flipout)

但我似乎無法使代碼工作(也許我缺少要導入的文件或東西)。此外,我需要一種方法來使這個工作與iOS!

回答

0

居然發現我需要什麼用我自己!

這裏的鏈接,有興趣的人士:https://gist.github.com/flarb/3252857

(我並不需要iOS的最終檢查,因爲我們的應用程序僅支持在平板電腦上,無論如何,再加上我認爲在所有iOS設備的默認方向是肖像(可能是錯的))

乾杯!

+0

明天只能接受我的回答。會這樣做。 – Bypp

2

對於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

+0

我應該補充一下,如果你已經明確地將activity的方向設置爲橫向/縱向,那麼'getRotation()'的值將不會考慮到這一點,並返回錯誤的「自然」方向(這將是任何你已經要求定位)。 – strangetimes

+1

你的'旋轉'是恆定的0,1,2或3,它們是Surface.ROTATION_0,90,180,270。因此你的switch語句需要被編輯。請參閱:https://developer.android.com/reference/android/view/Surface.html#ROTATION_0 –

相關問題