Skip to content

Instantly share code, notes, and snippets.

@Sin-Gala
Created June 21, 2022 20:22
Show Gist options
  • Save Sin-Gala/9a8c300b572b7a30fc2fc74dc0fa3493 to your computer and use it in GitHub Desktop.
Save Sin-Gala/9a8c300b572b7a30fc2fc74dc0fa3493 to your computer and use it in GitHub Desktop.
Screen and Orientation checks events for Unity
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);
}
}
// 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