Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Screen Orientation Not Changing

Discussion in 'Android' started by m00nkeh, Oct 16, 2022.

  1. m00nkeh

    m00nkeh

    Joined:
    Jun 29, 2021
    Posts:
    3
    I am fairly new to Unity so I am probably making a simple mistake, but I'm trying to change screen orientation for a simple 2D scene and it is not working.

    I have written the following test script that is attached to the main camera:
    Code (CSharp):
    1. void Start()
    2.     {  
    3.  
    4.         Debug.Log("Orientation: " + Screen.orientation.ToString());    
    5.         Screen.orientation = ScreenOrientation.LandscapeLeft;
    6.         Debug.Log("landscape left: " + Screen.orientation.ToString());    
    7.         Screen.orientation = ScreenOrientation.LandscapeRight;
    8.         Debug.Log("landscape right: " + Screen.orientation.ToString());    
    9.         Screen.orientation = ScreenOrientation.Portrait;
    10.         Debug.Log("portrait: " + Screen.orientation.ToString());    
    11.         Screen.orientation = ScreenOrientation.PortraitUpsideDown;
    12.         Debug.Log("portrait UD: " + Screen.orientation.ToString());
    13. }
    This is what appears in Android Logcat:

    2022/10/16 16:02:24.815 22614 22638 Info Unity Orientation: Portrait
    2022/10/16 16:02:24.815 22614 22638 Info Unity #0 0xc6774cc5 (libunity.so) ? 0x0
    2022/10/16 16:02:24.815 22614 22638 Info Unity #1 0xc6cff4fb (libunity.so) ? 0x0
    2022/10/16 16:02:24.815 22614 22638 Info Unity #2 0xc6436c6b (libunity.so) ? 0x0
    2022/10/16 16:02:24.815 22614 22638 Info Unity #3 0xc6436bb5 (libunity.so) ? 0x0
    2022/10/16 16:02:24.815 22614 22638 Info Unity #4 0xd7ccaaa2 (Unknown) ? 0x0
    2022/10/16 16:02:24.815 22614 22638 Info Unity
    2022/10/16 16:02:24.815 22614 22638 Info Unity landscape left: Portrait
    2022/10/16 16:02:24.815 22614 22638 Info Unity #0 0xc6774cc5 (libunity.so) ? 0x0
    2022/10/16 16:02:24.815 22614 22638 Info Unity #1 0xc6cff4fb (libunity.so) ? 0x0
    2022/10/16 16:02:24.815 22614 22638 Info Unity #2 0xc6436c6b (libunity.so) ? 0x0
    2022/10/16 16:02:24.815 22614 22638 Info Unity #3 0xc6436bb5 (libunity.so) ? 0x0
    2022/10/16 16:02:24.815 22614 22638 Info Unity #4 0xd7ccaaa2 (Unknown) ? 0x0
    2022/10/16 16:02:24.815 22614 22638 Info Unity
    2022/10/16 16:02:24.816 22614 22638 Info Unity landscape right: Portrait
    2022/10/16 16:02:24.816 22614 22638 Info Unity #0 0xc6774cc5 (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #1 0xc6cff4fb (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #2 0xc6436c6b (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #3 0xc6436bb5 (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #4 0xd7ccaaa2 (Unknown) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity
    2022/10/16 16:02:24.816 22614 22638 Info Unity portrait: Portrait
    2022/10/16 16:02:24.816 22614 22638 Info Unity #0 0xc6774cc5 (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #1 0xc6cff4fb (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #2 0xc6436c6b (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #3 0xc6436bb5 (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #4 0xd7ccaaa2 (Unknown) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity
    2022/10/16 16:02:24.816 22614 22638 Info Unity portrait UD: Portrait
    2022/10/16 16:02:24.816 22614 22638 Info Unity #0 0xc6774cc5 (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #1 0xc6cff4fb (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #2 0xc6436c6b (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #3 0xc6436bb5 (libunity.so) ? 0x0
    2022/10/16 16:02:24.816 22614 22638 Info Unity #4 0xd7ccaaa2 (Unknown) ? 0x0


    This is running on an ASUS ROG Phone 5 via USB debugging. What am I doing wrong?
     
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,681
    You're calling all those calls in single function, meaning one frame. You cannot change orientations 4 times in one frame.
     
  3. m00nkeh

    m00nkeh

    Joined:
    Jun 29, 2021
    Posts:
    3
    That's not the issue; it still doesn't work when only called once.
     
  4. m00nkeh

    m00nkeh

    Joined:
    Jun 29, 2021
    Posts:
    3
    After some further testing, it seems that the orientation takes several frames to update.

    This code:
    Code (CSharp):
    1.     void Start()
    2.     {  
    3.         Debug.Log("Initial Orientation: " + Screen.orientation.ToString());    
    4.         Screen.orientation = ScreenOrientation.LandscapeLeft;      
    5.     }  
    6.     void Update()
    7.     {      
    8.         Debug.Log("Orientation: " + Screen.orientation.ToString());
    9.     }
    Gives this output:
    2022/10/17 11:58:48.499 29491 29523 Info Unity Initial Orientation: Portrait
    2022/10/17 11:58:48.499 29491 29523 Info Unity #0 0xc6672cc5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.499 29491 29523 Info Unity #1 0xc6bfd4fb (libunity.so) ? 0x0
    2022/10/17 11:58:48.499 29491 29523 Info Unity #2 0xc6334c6b (libunity.so) ? 0x0
    2022/10/17 11:58:48.499 29491 29523 Info Unity #3 0xc6334bb5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.499 29491 29523 Info Unity #4 0xd7cca4ea (Unknown) ? 0x0
    2022/10/17 11:58:48.499 29491 29523 Info Unity
    2022/10/17 11:58:48.509 29491 29523 Info Unity Orientation: Portrait
    2022/10/17 11:58:48.509 29491 29523 Info Unity #0 0xc6672cc5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.509 29491 29523 Info Unity #1 0xc6bfd4fb (libunity.so) ? 0x0
    2022/10/17 11:58:48.509 29491 29523 Info Unity #2 0xc6334c6b (libunity.so) ? 0x0
    2022/10/17 11:58:48.509 29491 29523 Info Unity #3 0xc6334bb5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.509 29491 29523 Info Unity #4 0xd7cca4ea (Unknown) ? 0x0
    2022/10/17 11:58:48.509 29491 29523 Info Unity
    2022/10/17 11:58:48.574 29491 29523 Info Unity Orientation: Portrait
    2022/10/17 11:58:48.574 29491 29523 Info Unity #0 0xc6672cc5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.574 29491 29523 Info Unity #1 0xc6bfd4fb (libunity.so) ? 0x0
    2022/10/17 11:58:48.574 29491 29523 Info Unity #2 0xc6334c6b (libunity.so) ? 0x0
    2022/10/17 11:58:48.574 29491 29523 Info Unity #3 0xc6334bb5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.574 29491 29523 Info Unity #4 0xd7cca4ea (Unknown) ? 0x0
    2022/10/17 11:58:48.574 29491 29523 Info Unity
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue Handler (android.hardware.display.DisplayManagerGlobal$DisplayListenerDelegate) {47f5586} sending message to a Handler on a dead thread
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue java.lang.IllegalStateException: Handler (android.hardware.display.DisplayManagerGlobal$DisplayListenerDelegate) {47f5586} sending message to a Handler on a dead thread
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.os.MessageQueue.enqueueMessage(MessageQueue.java:560)
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.os.Handler.enqueueMessage(Handler.java:778)
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.os.Handler.sendMessageAtTime(Handler.java:727)
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.os.Handler.sendMessageDelayed(Handler.java:697)
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.os.Handler.sendMessage(Handler.java:635)
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.hardware.display.DisplayManagerGlobal$DisplayListenerDelegate.sendDisplayEvent(DisplayManagerGlobal.java:722)
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.hardware.display.DisplayManagerGlobal.handleDisplayEvent(DisplayManagerGlobal.java:351)
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.hardware.display.DisplayManagerGlobal.access$200(DisplayManagerGlobal.java:55)
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.hardware.display.DisplayManagerGlobal$DisplayManagerCallback.onDisplayEvent(DisplayManagerGlobal.java:708)
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.hardware.display.IDisplayManagerCallback$Stub.onTransact(IDisplayManagerCallback.java:84)
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.os.Binder.execTransactInternal(Binder.java:1159)
    2022/10/17 11:58:48.576 29491 29507 Warn MessageQueue at android.os.Binder.execTransact(Binder.java:1123)
    2022/10/17 11:58:48.605 29491 29523 Info Unity Orientation: Portrait
    2022/10/17 11:58:48.605 29491 29523 Info Unity #0 0xc6672cc5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.605 29491 29523 Info Unity #1 0xc6bfd4fb (libunity.so) ? 0x0
    2022/10/17 11:58:48.605 29491 29523 Info Unity #2 0xc6334c6b (libunity.so) ? 0x0
    2022/10/17 11:58:48.605 29491 29523 Info Unity #3 0xc6334bb5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.605 29491 29523 Info Unity #4 0xd7cca4ea (Unknown) ? 0x0
    2022/10/17 11:58:48.605 29491 29523 Info Unity
    2022/10/17 11:58:48.619 29491 29626 Info SwappyDisplayManager Terminating looper thread
    2022/10/17 11:58:48.632 29491 29751 Info SwappyDisplayManager Starting looper thread
    2022/10/17 11:58:48.632 29491 29523 Verbose SwappyVk SwappyVk initialized for VkDevice 0xbede1870 using VK_GOOGLE_display_timing on Android
    2022/10/17 11:58:48.632 29491 29523 Info SwappyVk Returning refresh duration of 16666666 nsec (approx 60.000002 Hz)
    2022/10/17 11:58:48.638 29491 29523 Info Unity Orientation: Landscape
    2022/10/17 11:58:48.638 29491 29523 Info Unity #0 0xc6672cc5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.638 29491 29523 Info Unity #1 0xc6bfd4fb (libunity.so) ? 0x0
    2022/10/17 11:58:48.638 29491 29523 Info Unity #2 0xc6334c6b (libunity.so) ? 0x0
    2022/10/17 11:58:48.638 29491 29523 Info Unity #3 0xc6334bb5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.638 29491 29523 Info Unity #4 0xd7cca4ea (Unknown) ? 0x0
    2022/10/17 11:58:48.638 29491 29523 Info Unity
    2022/10/17 11:58:48.671 29491 29523 Info Unity Orientation: Landscape
    2022/10/17 11:58:48.671 29491 29523 Info Unity #0 0xc6672cc5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.671 29491 29523 Info Unity #1 0xc6bfd4fb (libunity.so) ? 0x0
    2022/10/17 11:58:48.671 29491 29523 Info Unity #2 0xc6334c6b (libunity.so) ? 0x0
    2022/10/17 11:58:48.671 29491 29523 Info Unity #3 0xc6334bb5 (libunity.so) ? 0x0
    2022/10/17 11:58:48.671 29491 29523 Info Unity #4 0xd7cca4ea (Unknown) ? 0x0


    Is this the expected behaviour?
     
  5. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,681
    Yes, that's expected.