using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Profiling; namespace Coffee.UIExtensions { internal static class UIParticleUpdater { static readonly List s_ActiveParticles = new List(); private static int frameCount = 0; public static void Register(UIParticle particle) { if (!particle) return; s_ActiveParticles.Add(particle); } public static void Unregister(UIParticle particle) { if (!particle) return; s_ActiveParticles.Remove(particle); } #if UNITY_EDITOR [UnityEditor.InitializeOnLoadMethod] #endif [RuntimeInitializeOnLoadMethod] private static void InitializeOnLoad() { Canvas.willRenderCanvases -= Refresh; Canvas.willRenderCanvases += Refresh; } private static void Refresh() { // Do not allow it to be called in the same frame. if (frameCount == Time.frameCount) return; frameCount = Time.frameCount; Profiler.BeginSample("[UIParticle] Refresh"); for (var i = 0; i < s_ActiveParticles.Count; i++) { try { s_ActiveParticles[i].UpdateRenderers(); } catch (Exception e) { Debug.LogException(e); } } Profiler.EndSample(); } } }