Advertisement
dnnkeeper

LandscapePortraitStateToggler

Feb 11th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.22 KB | None | 0 0
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4.  
  5. public class LandscapePortraitStateToggler : MonoBehaviour {
  6.  
  7.     public UnityEvent onPortraitMode;
  8.     public UnityEvent onLandscapeMode;
  9.  
  10.     public static float CheckDelay = 0.5f;        // How long to wait until we check again.
  11.  
  12.     static DeviceOrientation orientation;        // Current Device Orientation
  13.     static bool isAlive = true;                    // Keep this script running?
  14.  
  15.     void Start() {
  16.         StartCoroutine(CheckForChange());
  17.     }
  18.  
  19.     IEnumerator CheckForChange(){
  20.         orientation = Input.deviceOrientation;
  21.  
  22.         while (isAlive) {
  23.  
  24.             // Check for an Orientation Change
  25.             switch (Input.deviceOrientation) {
  26.             case DeviceOrientation.Unknown:            // Ignore
  27.             case DeviceOrientation.FaceUp:            // Ignore
  28.             case DeviceOrientation.FaceDown:        // Ignore
  29.                 break;
  30.             default:
  31.                 if (orientation != Input.deviceOrientation) {
  32.                     orientation = Input.deviceOrientation;
  33.                     if (orientation == DeviceOrientation.Portrait)
  34.                         onPortraitMode.Invoke ();
  35.                     else
  36.                         onLandscapeMode.Invoke ();
  37.                 }
  38.                 break;
  39.             }
  40.  
  41.             yield return new WaitForSeconds(CheckDelay);
  42.         }
  43.     }
  44.  
  45.     void OnDestroy(){
  46.         isAlive = false;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement