IngameResolutionMonitor.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. namespace TheraBytes.BetterUi
  8. {
  9. [HelpURL("https://documentation.therabytes.de/better-ui/IngameResolutionMonitor.html")]
  10. [AddComponentMenu("Better UI/In-Game Resolution Monitor", 30)]
  11. public class IngameResolutionMonitor : MonoBehaviour
  12. {
  13. static IngameResolutionMonitor instance;
  14. [SerializeField] bool onlyPresentInThisScene= false;
  15. public static GameObject Create()
  16. {
  17. GameObject go = new GameObject("IngameResolutionMonitor");
  18. go.AddComponent<IngameResolutionMonitor>();
  19. return go;
  20. }
  21. private void OnEnable()
  22. {
  23. if(instance != null)
  24. {
  25. Debug.LogWarning("There already is an Ingame Resolution Monitor. One is enough. Destroying the previous one now...");
  26. GameObject.Destroy(instance.gameObject);
  27. }
  28. instance = this;
  29. if (!onlyPresentInThisScene)
  30. {
  31. GameObject.DontDestroyOnLoad(this.gameObject);
  32. }
  33. SceneManager.sceneLoaded += SceneLoaded;
  34. }
  35. private void OnDisable()
  36. {
  37. instance = null;
  38. SceneManager.sceneLoaded -= SceneLoaded;
  39. }
  40. private void SceneLoaded(Scene scene, LoadSceneMode mode)
  41. {
  42. ResolutionMonitor.MarkDirty();
  43. ResolutionMonitor.Update();
  44. }
  45. #if !(UNITY_EDITOR)
  46. void Update()
  47. {
  48. ResolutionMonitor.Update();
  49. }
  50. #endif
  51. }
  52. }