Search code examples
androidunity-game-enginescreen-orientation

Device Orientation Carry Over


I'm using Unity 3D to create a simple menu. I couldn't get ancors so instead, I've used two Canvas with different layouts according to Landscape and Portrait.

This works perfect for me. BUT.

I have to properly set the device in that orientation for it to properly register.

To explain better: I've put Landscape values as default in Start() and in Update() I have the checkandchanger() which checks the orientation and changes it to the appropriate layout.

So now, if I have my device on Portrait and I change scenes, and if my device isn't perfectly in that orientation (straight up) it loads the Landscape.

Hence, coming to my question, is it possible to send orientation of the previous scene to the next scene?


Solution

  • Yes, it is. Create an empty gameobject named "OrientationManger". Attach a simple script to it and make sure that this script doesn't destroy when loading the next scene. This script will have public functions that sets the current orientation or reads the last orientation from the previous scene.

    You can then reference those values from both scene 1 and scene 2.

    To prevent this "OrientationManger" gameobject and the script attached to it from destroying when loading the next scene, use:

    use

    void Awake() {
            DontDestroyOnLoad(this.gameObject);
        }
    

    EDIT: This is what the OrientationManager should look like. You can choose any method of doing it.

    public class OrientationManger : MonoBehaviour
    {
        private OrientationType currentOrientation; ///////////////Method 1 (ENUM)
        int orientationType = 1; ///////////////Method 2 (OR use Integer 1,2)
    
        //Makes sure there is only once instance running
        void Awake ()
        {
            // singleton
            if (FindObjectsOfType (typeof(OrientationManger)).Length > 1) {
                Debug.Log ("OrientationManger already exist");
                DestroyImmediate (gameObject); //Destroy this gameobject because it already exist
            } else {
                DontDestroyOnLoad (this.gameObject); //Wont destroy when loading another level/scene
            }
        }
    
        ///////////////Method 1 Set (ENUM)
        public void setOrientation (OrientationType orientType)
        {
            currentOrientation = orientType;
        }
    
        ///////////////Method 1 Get (ENUM)
        public OrientationType getOrientation ()
        {
            return currentOrientation;
        }
    
        ///////////////Method 2 Set (OR use Integer 1,2)
        public void setOrientation2 (int orientType)
        {
            if (orientType == 1) {
                orientationType = orientType;
            } else if (orientType == 2) {
                orientationType = orientType;
            }
        }
    
        ///////////////Method 2 Get (OR use Integer 1,2)
        public int getOrientation2 ()
        {
            if (orientationType == 1 || orientationType == 2) {
                return orientationType;
            } else {
                return 0;
            }
        }
    
    }
    
    ///////////////Method 1 (ENUM)
    public enum OrientationType
    {
        UKNOWN,
        Portrait,
        Landscape
    }
    ;