using System; using System.Collections; using System.Collections.Generic; using UnityEngine; //Передавать Action по добавлению и удалению из пула public class ObjectPool : IEnumerable where T : MonoBehaviour { public int Capacity { get; private set; } private Func actionOnCreate; private Action actionOnGet; private Action actionOnRelease; private List pool; public ObjectPool(Func actionOnCreate, Action actionOnGet, Action actionOnRelease, int initialCapacity) { Capacity = initialCapacity; this.actionOnCreate = actionOnCreate; this.actionOnGet = actionOnGet; this.actionOnRelease = actionOnRelease; pool = new List(); for (uint i = 0; i < Capacity; i++) { var obj = actionOnCreate(); pool.Add(obj); } } public T this[int i] { get => pool[i]; set => pool[i] = value; } public T TryGetFromPos(in Vector3 pos,bool isActive) { foreach (var obj in pool) { if (isActive) { if (obj.gameObject.activeInHierarchy && pos == obj.gameObject.transform.position) { actionOnGet.Invoke(obj); return obj; } } else { if (!obj.gameObject.activeInHierarchy && pos == obj.gameObject.transform.position) { actionOnGet.Invoke(obj); return obj; } } } return null; } public T Get() { if (TryGet(out var element)) { return element; } T instance = ExpandPool(); instance.gameObject.SetActive(true); return instance; } private bool TryGet(out T element) { List inactiveObjects = GetInactiveElements(); if (inactiveObjects.Count > 0) { var obj = inactiveObjects[UnityEngine.Random.Range(0, inactiveObjects.Count)]; element = obj; actionOnGet.Invoke(element); return true; } element = null; return false; } public List GetInactiveElements() { return pool.FindAll(obj => !obj.gameObject.activeInHierarchy); } public List GetActiveElements() { return pool.FindAll(obj => obj.gameObject.activeInHierarchy); } public T ExpandPool() { var obj = actionOnCreate(); Capacity++; pool.Add(obj); return obj; } public void ReturnToPool(T obj) { if (obj == null) return; if (obj.gameObject.activeInHierarchy) { actionOnRelease.Invoke(obj); } return; } public IEnumerator GetEnumerator() { return pool.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } }