|
| 1 | +using UnityEngine; |
| 2 | + |
| 3 | +namespace Zigurous.UI |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// Listens for changes in the screen size. |
| 7 | + /// </summary> |
| 8 | + public sealed class ScreenSizeListener : MonoBehaviour |
| 9 | + { |
| 10 | + private static volatile ScreenSizeListener _instance; |
| 11 | + private static object _lock = new object(); |
| 12 | + private static bool _isUnloading = false; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// The current instance of the class. |
| 16 | + /// The instance will be created if it does not already exist. |
| 17 | + /// </summary> |
| 18 | + public static ScreenSizeListener Instance |
| 19 | + { |
| 20 | + get |
| 21 | + { |
| 22 | + if (_isUnloading) { |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + if (_instance == null) |
| 27 | + { |
| 28 | + lock (_lock) |
| 29 | + { |
| 30 | + _instance = FindObjectOfType<ScreenSizeListener>(); |
| 31 | + |
| 32 | + if (_instance == null) |
| 33 | + { |
| 34 | + GameObject singleton = new GameObject(); |
| 35 | + singleton.name = typeof(ScreenSizeListener).Name; |
| 36 | + singleton.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector; |
| 37 | + singleton.AddComponent<ScreenSizeListener>(); |
| 38 | + DontDestroyOnLoad(singleton); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return _instance; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Checks if the singleton has been initialized and an instance is |
| 49 | + /// available to use. |
| 50 | + /// </summary> |
| 51 | + public static bool HasInstance => _instance != null; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// A function delegate invoked when the screen size changes. |
| 55 | + /// </summary> |
| 56 | + /// <param name="width">The new width of the screen.</param> |
| 57 | + /// <param name="height">The new height of the screen.</param> |
| 58 | + public delegate void OnResize(int width, int height); |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// The callback invoked when the screen size changes. |
| 62 | + /// </summary> |
| 63 | + public OnResize resized; |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// The current width of the screen. |
| 67 | + /// </summary> |
| 68 | + public int width { get; private set; } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// The current height of the screen. |
| 72 | + /// </summary> |
| 73 | + public int height { get; private set; } |
| 74 | + |
| 75 | + private ScreenSizeListener() {} |
| 76 | + |
| 77 | + private void Awake() |
| 78 | + { |
| 79 | + _isUnloading = false; |
| 80 | + |
| 81 | + if (_instance == null) { |
| 82 | + _instance = this; |
| 83 | + } else { |
| 84 | + Destroy(this); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private void OnDestroy() |
| 89 | + { |
| 90 | + this.resized = null; |
| 91 | + |
| 92 | + _isUnloading = true; |
| 93 | + |
| 94 | + if (_instance == this) { |
| 95 | + _instance = null; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private void Update() |
| 100 | + { |
| 101 | + if (Screen.width != this.width || Screen.height != this.height) |
| 102 | + { |
| 103 | + this.width = Screen.width; |
| 104 | + this.height = Screen.height; |
| 105 | + |
| 106 | + if (this.resized != null) { |
| 107 | + this.resized.Invoke(this.width, this.height); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments