ScreenOrientation.PortraitUpsideDown not working
I want to set orientation of device to PortraitUpsideDown. For this purpose, I have written some test code. Here it is :
void Update ()
{
if (Input.deviceOrientation == DeviceOrientation.LandscapeLeft && Screen.orientation != ScreenOrientation.LandscapeLeft) {
Debug.Log ("landscape left");
Screen.orientation = ScreenOrientation.LandscapeLeft;
} else if (Input.deviceOrientation == DeviceOrientation.LandscapeRight && Screen.orientation != ScreenOrientation.LandscapeRight) {
Debug.Log ("landscape right");
Screen.orientation = ScreenOrientation.LandscapeRight;
} else if (Input.deviceOrientation == DeviceOrientation.Portrait && Screen.orientation != ScreenOrientation.Portrait) {
Debug.Log ("portrait");
Screen.orientation = ScreenOrientation.Portrait;
} else if (Input.deviceOrientation == DeviceOrientation.PortraitUpsideDown && Screen.orientation != ScreenOrientation.PortraitUpsideDown) {
Debug.Log ("portrait upside down");
Screen.orientation = ScreenOrientation.PortraitUpsideDown;
}
}
Here in this all orientations condition become true only for 2 times. While PortraitUpsideDown become keep on running when I set my device to that orientation.
Why it is not running as like other orientations code?? Please give me your suggestion for this. Is it possible android device not contains that orientation?
you wrote 16 lines of code to perform a task that could be done by just setting Player settings >> Default Orientation
to Auto Rotation
... WHY ?
Just to make sure: did you enable all the wanted screen orientations in the player settings? Otherwise the app will be build without the ability to use them.
Answer by aditya · Apr 15, 2016 at 11:59 AM
remove else if
and use only if
or even better use switch
and even best just set Player Settings >> Resolutions and Presentation >> Default Orientation
to Auto Rotation
Basically I want to detect orientation change in my game. Because for landscape and portrait, I have different game play views. Something similar to Candy Crush. Now give me suggestion for above.
then simply set the Default screen orientation in Player settings to Auto Rotation
and through script just check what is the current orientation using Screen.Orientation
, coz of this Unity will handle your Orientations and you just had to make two variables one for current orientation and other for previous orientation ... if this helps please accept the ANSWER
I am already going to up vote this answer because of time as well proper reply. I am implementing same so get back to you.