using System; using System.Collections.Generic; using Reflex.Attributes; using Unity.VisualScripting; using UnityEngine; using UnityEngine.Pool; using Object = UnityEngine.Object; namespace Code.Infrastructure.Particles { public class ParticlePool { public IObjectPool Pool; private ParticleInfo _particleInfoConfig; private ParticleSystem _particleSystemPrefab; public ParticlePool(ParticleInfo particleInfoConfig, ParticleSystem particleSystemPrefab) { _particleSystemPrefab = particleSystemPrefab; _particleInfoConfig = particleInfoConfig; Pool = new ObjectPool( createFunc: CreateItem, // actionOnGet: OnGet, actionOnRelease: OnRelease, actionOnDestroy: OnDestroyItem, collectionCheck: true, // helps catch double-release mistakes defaultCapacity: particleInfoConfig.CountInPool, maxSize: 20 ); } public ParticleSystem Get() => Pool.Get(); public void Release(ParticleSystem particleSystem) => Pool.Release(particleSystem); // Creates a new pooled GameObject the first time (and whenever the pool needs more). private ParticleSystem CreateItem() { var particleSystem = Object .Instantiate(_particleSystemPrefab) // Он уже загружен .GetComponent(); particleSystem.name = "PooledParticleSystem"; Object.DontDestroyOnLoad(particleSystem); return particleSystem; } // Called when an item is taken from the pool. private void OnGet(ParticleSystem source) { // Настроить если надо } // Called when an item is returned to the pool. private void OnRelease(ParticleSystem particleSystem) { if (!particleSystem) return; particleSystem.Stop(); // Настроить если надо } // Called when the pool decides to destroy an item (e.g., above max size). private void OnDestroyItem(ParticleSystem source) { Object.Destroy(source.gameObject); } } public interface IParticlePoolManagerForService { ParticleSystem GetParticleSystem(ParticlePartition partition, int id); void ReleaseParticleSystem(ParticlePartition partition, int id, ParticleSystem particleSystem); } public interface IParticlePoolManagerForFabric { void CreatePoolOfParticles(ParticleInfo particleInfoConfig, ParticleSystem particleSystemPrefab); void DeletePoolOfParticles(ParticlePartition partition, int id); } public class ParticlePoolManager : IParticlePoolManagerForService, IParticlePoolManagerForFabric { // The pool holds plain GameObjects (you can swap this for any component type). private Dictionary> _poolDictionary = new(); public void CreatePoolOfParticles(ParticleInfo particleInfoConfig, ParticleSystem particleSystemPrefab) { if(!_poolDictionary.ContainsKey(particleInfoConfig.particlePartition)) _poolDictionary[particleInfoConfig.particlePartition] = new(); _poolDictionary[particleInfoConfig.particlePartition] .Add(particleInfoConfig.particleId, new ParticlePool(particleInfoConfig, particleSystemPrefab)); } public void DeletePoolOfParticles(ParticlePartition partition, int id) { _poolDictionary[partition][id].Pool.Clear(); _poolDictionary[partition].Remove(id); } public ParticleSystem GetParticleSystem(ParticlePartition partition, int id) { var source = _poolDictionary[partition][id].Get(); // Настроить если надо return source; } public void ReleaseParticleSystem(ParticlePartition partition, int id, ParticleSystem particleSystem) { if(_poolDictionary[partition].ContainsKey(id)) _poolDictionary[partition][id].Release(particleSystem); } } }