Screen Orientation Not Working.

Hi, I am new to Unity, I am making a small 2d game where all Scenes are in Portrait, The new Scene i want to change the screen orientation to Landscape, the code i found in many Forums and Discussion is:

void Start()
{
Screen.autorotateToLandscapeLeft = false;
Screen.autorotateToLandscapeRight = false;
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
Screen.orientation = ScreenOrientation.LandscapeLeft;
Debug.Log(Screen.orientation.ToString());

}

I did try it in a simple C# script in the Start() method. and Attached the script to main camera, and many different objects but none work for me. When i Debug.Log() the screen orientation still displays Portrait.
The default orientation in project settings is Portrait. I am using Unity Remote 5 for testing it on my iPhone.
I did search in many forums and read Unity Scripts but still its not working… Any help will be really appreciated…

1 Like

try from scratch using just this one here.
void Start()
{
Screen.orientation = ScreenOrientation.LandscapeLeft;
}

Thanks reigita,
I guess the problem was, it will not work in the editor it will work once i deploy it in my mobile. I think unity should give this option to change and see orientation during development.

Download the Device Simulator from preview packages

1 Like

third and final place I will say this even though the previous comment was probably a better answer:

so i was just wanting a quick easy way to view how my project might look based on orientation in the Unity editor before i built my project.

i came up with this method hack to still be able to get the desired results in the editor…

         public ScreenOrientation myScreenOrientation {
             get{
                #if UNITY_EDITOR
                    if(Screen.height > Screen.width){
                        return ScreenOrientation.Portrait;
                    } else {
                        return ScreenOrientation.Landscape;
                    }
                #else
                    return Screen.orientation;
                #endif
             }
         }

then instead of using Screen.orientation i use myScreenOrientation

I did not do this for Input.deviceOrientation because this may not be the same as what the screen orientation is… for example they could be laying in a bed with the phone overhead and it would read as facedown which is nether landscape or portrait and did not help me with my ui layouts

1 Like