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

[SOLVED] Handling Android phone auto rotation orientation changes

Discussion in '2D' started by Kurius, Jan 19, 2016.

  1. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412


    The video above shows buggy behavior, but the latest version of Unity eliminates those bugs.
    If anybody is interested, the code below works perfectly for handling orientation changes. Just add this script to your camera.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class myOrientationHandler : MonoBehaviour {
    5.     //ASSUMING YOUR CAMERA IS INITIALLY SETUP IN LANDSCAPE MODE
    6.     //THIS SCRIPT ADJUSTS THE CAMERA IF DEVICE IS ROTATED TO PORTRAIT
    7.  
    8.     float landscapeSize;
    9.     bool isPortrait = false;
    10.  
    11.     void Awake () {
    12.         landscapeSize = Camera.main.orthographicSize;
    13.         StartCoroutine (myCheckOrientation ());
    14.     }
    15.  
    16.     IEnumerator myCheckOrientation(){
    17.         yield return new WaitForSeconds(0.5f);
    18.         if (!isPortrait && Camera.main.aspect <= 1) {
    19.             isPortrait = true;
    20.             print ("PORTRAIT MODE !!!");
    21.             Camera.main.orthographicSize = landscapeSize / Camera.main.aspect;
    22.         } else if (isPortrait && Camera.main.aspect > 1) {
    23.             isPortrait = false;
    24.             print ("LANDSCAPE MODE !!!");
    25.             Camera.main.orthographicSize = landscapeSize;
    26.         }
    27.         StartCoroutine (myCheckOrientation ());
    28.     }
    29.  
    30. }
    31.  
     
    Last edited: Jan 20, 2016
  2. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    SOLVED by updating to the latest version of Unity (v5.3.1f1).
    Moderators, please remove "[BUG]" from title of this thread. Thank you.