Bypp
Bypp

Reputation: 331

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. What is the best way to achieve this through code (I'm working with c#) ?

Thanks for the help !

Note : I've seen the following post : How to check device natural (default) orientation on Android (i.e. get landscape for e.g., Motorola Charm or 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 !

Upvotes: 0

Views: 2406

Answers (2)

Bypp
Bypp

Reputation: 331

Actually found what I needed by myself !

Here's the link, for anyone interested : 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))

Cheers !

Upvotes: 0

Zeus
Zeus

Reputation: 432

For Android you could use 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 :

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

Upvotes: 2

Related Questions