Created
June 21, 2022 20:22
-
-
Save Sin-Gala/9a8c300b572b7a30fc2fc74dc0fa3493 to your computer and use it in GitHub Desktop.
Screen and Orientation checks events for Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
public class ScreenOrientationCheck | |
{ | |
public static event Action<ScreenOrientation> onOrientationChange; | |
public static ScreenOrientation lastOrientation; | |
[RuntimeInitializeOnLoadMethod] | |
private static void Init() | |
{ | |
lastOrientation = Screen.orientation; | |
OrientationChanged(lastOrientation); | |
Debug.Log("Orientation: " + lastOrientation.ToString()); | |
} | |
public static void CheckNewOrientation() //To call from Update in a Mono-Behaviour | |
{ | |
if (Screen.orientation == lastOrientation) return; | |
lastOrientation = Screen.orientation; | |
OrientationChanged(lastOrientation); | |
} | |
private static void OrientationChanged(ScreenOrientation newOrientation) | |
{ | |
if (onOrientationChange != null) | |
onOrientationChange(newOrientation); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on: https://forum.unity.com/threads/window-resize-event.40253/ | |
using System; | |
using UnityEngine; | |
public static class ScreenSizeCheck | |
{ | |
public static event Action onScreenSizeChange; | |
public static Vector2 lastScreenSize { get; private set; } | |
[RuntimeInitializeOnLoadMethod] | |
private static void Init() | |
{ | |
lastScreenSize = new Vector2(Screen.width, Screen.height); | |
Debug.Log("Screen Size: " + lastScreenSize); | |
} | |
public static void CheckNewScreenSize() //To call from Update in a Mono-Behaviour | |
{ | |
Vector2 screenSize = new Vector2(Screen.width, Screen.height); | |
if (lastScreenSize == screenSize) return; | |
lastScreenSize = screenSize; | |
ScreenSizeChanged(); | |
} | |
private static void ScreenSizeChanged() | |
{ | |
if (onScreenSizeChange != null) | |
onScreenSizeChange(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment