Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using UnityEngine;
- using UnityEngine.Events;
- public class LandscapePortraitStateToggler : MonoBehaviour {
- public UnityEvent onPortraitMode;
- public UnityEvent onLandscapeMode;
- public static float CheckDelay = 0.5f; // How long to wait until we check again.
- static DeviceOrientation orientation; // Current Device Orientation
- static bool isAlive = true; // Keep this script running?
- void Start() {
- StartCoroutine(CheckForChange());
- }
- IEnumerator CheckForChange(){
- orientation = Input.deviceOrientation;
- while (isAlive) {
- // Check for an Orientation Change
- switch (Input.deviceOrientation) {
- case DeviceOrientation.Unknown: // Ignore
- case DeviceOrientation.FaceUp: // Ignore
- case DeviceOrientation.FaceDown: // Ignore
- break;
- default:
- if (orientation != Input.deviceOrientation) {
- orientation = Input.deviceOrientation;
- if (orientation == DeviceOrientation.Portrait)
- onPortraitMode.Invoke ();
- else
- onLandscapeMode.Invoke ();
- }
- break;
- }
- yield return new WaitForSeconds(CheckDelay);
- }
- }
- void OnDestroy(){
- isAlive = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement