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. Dismiss Notice

[Android] Broken device orientation

Discussion in 'Scripting' started by Qbit86, Aug 31, 2016.

  1. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Hi,

    I'm facing quite severe issue with device orientation and have no idea how to solve it.

    In Player Settings, Default Orientation is set to Auto Rotation with allowed options: Landscape Right, Landscape Left. In Android Manifest, main activity has `android:screenOrientation="sensorLandscape"`. But. When the user holds device with running game in portrait orientation, switches screen off, then unlocks it back — the content appears in wrong orientation for a moment, then rotates back to landscape orientation. However, the moment of being in prohibited portrait orientation is enough for NGUI to break layout. Looks like anchors or smth like that have time to set wrong positions.

    Is there any way to suppress undesired orientation or at least detect it in runtime so that I could reload the game in this scenario?

    Any help would be very much appreciated!

    Thanks.
    --
    Unity 5.3.6f1 Pro
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Like resolution changes, there are no built-in events that you can subscribe to in order to get updates on when the phone orientation changes, but you can make one yourself easily enough.

    In your game manager (singleton or static members+functions for this), just cache the value of the current Screen.orientation and check each frame if it changes to something different. If it does change, fire off an event at a public UnityEvent you keep there to all listeners. In your scripts that rely on the current orientation value, register a method to AddListener to the UnityEvent, then make your adjustments inside of that function.
     
  3. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    487
    Thanks, I will try. I've tried catching in `OnApplicationPause()`/`OnApplicationFocus()`, but they are likely postponed to next frame, when orientation is already restored; so they always report landscape orientation. `Update()` may work.
     
  4. redstonegames

    redstonegames

    Joined:
    Sep 19, 2015
    Posts:
    11
    I'm facing the same issue:

    My game is Portrait only. I have already set 'Default Orientation' to 'Portrait' in the Build Settings and also checked whether the final `com.unity3d.player.UnityPlayerActivity` activity contains `android:screenOrientation="portrait"`.

    Sometimes fullscreen ads (videos) are shown between scene transitions in my game. If the ad is presented in Landscape orientation, when it is dismissed I can see my game's UI in landscape in a short interval of time before being 'rotated back' to Portrait. The problem is that it causes some of my NGUI components to break the layout. I tried setting all anchors to update every frame, but without luck. I'm not sure if this is a Unity or a NGUI issue. But it's weird because I can see my game (thus, the Unity activity) in Landscape before the UI gets messed up.

    Have you found any solution for this issue Qbit86?

    INFO:
    - Unity 5.4.2p4
    - NGUI 3.10.0
     
  5. redstonegames

    redstonegames

    Joined:
    Sep 19, 2015
    Posts:
    11
    I was able to identify the problem by creating a custom `ScreenOrientationHelper` script that analyzes the ` Screen.width` and ` Screen.height` properties in the ` OnApplicationPause ()` method. I also changed the Script Execution Order of the script to ensure it runs before all other scripts in the application.

    As soon as control returns to the `com.unity3d.player.UnityPlayerActivity`, the `Screen.width` and ` Screen.height` properties are inverted (though `Screen.orientation` is preserved).

    However, I don't know how to fix this because `Screen.width` and ` Screen.height` properties are readonly (I tried assigning/restoring them before NGUI scripts run).

    Any idea?
     
  6. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    533
    Is there any solution? just faced similar problem