using System; using System.Runtime.CompilerServices; using UnityEngine; namespace ADOFAIRenderer.Util { /// Particle system helpers. internal static class ParticleUtil { /// /// Renderer found for each particle system. Weak keys: an entry is dropped when its ParticleSystem object is collected. /// Main thread. /// private static readonly ConditionalWeakTable Cache = new(); /// /// Returns the ParticleSystemRenderer that belongs to . When a GameObject has several particle /// systems and renderers, the renderer with the same component index as the particle system is used; otherwise the /// first renderer of the GameObject. The result is cached per particle system. Main thread. /// /// Particle system; null or destroyed returns null. /// The renderer, or null when the GameObject has none. public static ParticleSystemRenderer? GetRenderer(this ParticleSystem ps) { if (!ps) return null; if (Cache.TryGetValue(ps, out var renderer) && renderer) return renderer; var particleSystems = ps.GetComponents(); var renderers = ps.GetComponents(); var index = Array.IndexOf(particleSystems, ps); renderer = index >= 0 && index < renderers.Length ? renderers[index] : ps.GetComponent(); if (renderer) Cache.Add(ps, renderer); return renderer; } } }