Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Screen Orientation Not Working.

Discussion in 'Android' started by blackcode101, Jul 10, 2020.

  1. blackcode101

    blackcode101

    Joined:
    Jul 10, 2020
    Posts:
    6
    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...
     
    Salvador-Romero likes this.
  2. reigita

    reigita

    Joined:
    Apr 29, 2019
    Posts:
    2
    try from scratch using just this one here.
    void Start()
    {
    Screen.orientation = ScreenOrientation.LandscapeLeft;
    }
     
  3. blackcode101

    blackcode101

    Joined:
    Jul 10, 2020
    Posts:
    6
    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.
     
  4. LeGingerGamer

    LeGingerGamer

    Joined:
    Jan 13, 2017
    Posts:
    4
    Download the Device Simulator from preview packages
     
    CrandellWS likes this.
  5. CrandellWS

    CrandellWS

    Joined:
    Oct 31, 2015
    Posts:
    178
    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...

    Code (CSharp):
    1.          public ScreenOrientation myScreenOrientation {
    2.              get{
    3.                 #if UNITY_EDITOR
    4.                     if(Screen.height > Screen.width){
    5.                         return ScreenOrientation.Portrait;
    6.                     } else {
    7.                         return ScreenOrientation.Landscape;
    8.                     }
    9.                 #else
    10.                     return Screen.orientation;
    11.                 #endif
    12.              }
    13.          }

    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
     
    Last edited: Feb 25, 2021
    kloogens likes this.