UIExtraCallbacks.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Coffee.UIParticleInternal
  6. {
  7. /// <summary>
  8. /// Provides additional callbacks related to canvas and UI system.
  9. /// </summary>
  10. internal static class UIExtraCallbacks
  11. {
  12. private static bool s_IsInitializedAfterCanvasRebuild;
  13. private static readonly FastAction s_AfterCanvasRebuildAction = new FastAction();
  14. private static readonly FastAction s_LateAfterCanvasRebuildAction = new FastAction();
  15. private static readonly FastAction s_BeforeCanvasRebuildAction = new FastAction();
  16. private static readonly FastAction s_OnScreenSizeChangedAction = new FastAction();
  17. private static Vector2Int s_LastScreenSize;
  18. static UIExtraCallbacks()
  19. {
  20. Canvas.willRenderCanvases += OnBeforeCanvasRebuild;
  21. Logging.LogMulticast(typeof(Canvas), "willRenderCanvases", message: "ctor");
  22. }
  23. /// <summary>
  24. /// Event that occurs after canvas rebuilds.
  25. /// </summary>
  26. public static event Action onLateAfterCanvasRebuild
  27. {
  28. add => s_LateAfterCanvasRebuildAction.Add(value);
  29. remove => s_LateAfterCanvasRebuildAction.Remove(value);
  30. }
  31. /// <summary>
  32. /// Event that occurs before canvas rebuilds.
  33. /// </summary>
  34. public static event Action onBeforeCanvasRebuild
  35. {
  36. add => s_BeforeCanvasRebuildAction.Add(value);
  37. remove => s_BeforeCanvasRebuildAction.Remove(value);
  38. }
  39. /// <summary>
  40. /// Event that occurs after canvas rebuilds.
  41. /// </summary>
  42. public static event Action onAfterCanvasRebuild
  43. {
  44. add => s_AfterCanvasRebuildAction.Add(value);
  45. remove => s_AfterCanvasRebuildAction.Remove(value);
  46. }
  47. /// <summary>
  48. /// Event that occurs when the screen size changes.
  49. /// </summary>
  50. public static event Action onScreenSizeChanged
  51. {
  52. add => s_OnScreenSizeChangedAction.Add(value);
  53. remove => s_OnScreenSizeChangedAction.Remove(value);
  54. }
  55. /// <summary>
  56. /// Initializes the UIExtraCallbacks to ensure proper event handling.
  57. /// </summary>
  58. private static void InitializeAfterCanvasRebuild()
  59. {
  60. if (s_IsInitializedAfterCanvasRebuild) return;
  61. s_IsInitializedAfterCanvasRebuild = true;
  62. CanvasUpdateRegistry.IsRebuildingLayout();
  63. Canvas.willRenderCanvases += OnAfterCanvasRebuild;
  64. Logging.LogMulticast(typeof(Canvas), "willRenderCanvases",
  65. message: "InitializeAfterCanvasRebuild");
  66. }
  67. #if UNITY_EDITOR
  68. [InitializeOnLoadMethod]
  69. #endif
  70. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  71. private static void InitializeOnLoad()
  72. {
  73. Canvas.willRenderCanvases -= OnAfterCanvasRebuild;
  74. s_IsInitializedAfterCanvasRebuild = false;
  75. }
  76. /// <summary>
  77. /// Callback method called before canvas rebuilds.
  78. /// </summary>
  79. private static void OnBeforeCanvasRebuild()
  80. {
  81. var screenSize = new Vector2Int(Screen.width, Screen.height);
  82. if (s_LastScreenSize != screenSize)
  83. {
  84. if (s_LastScreenSize != default)
  85. {
  86. s_OnScreenSizeChangedAction.Invoke();
  87. }
  88. s_LastScreenSize = screenSize;
  89. }
  90. s_BeforeCanvasRebuildAction.Invoke();
  91. InitializeAfterCanvasRebuild();
  92. }
  93. /// <summary>
  94. /// Callback method called after canvas rebuilds.
  95. /// </summary>
  96. private static void OnAfterCanvasRebuild()
  97. {
  98. s_AfterCanvasRebuildAction.Invoke();
  99. s_LateAfterCanvasRebuildAction.Invoke();
  100. }
  101. }
  102. }