using UOP1.Factory; using System.Collections.Generic; using UnityEngine; namespace UOP1.Pool { /// /// A generic pool that generates members of type T on-demand via a factory. /// /// Specifies the type of elements to pool. public abstract class PoolSO : ScriptableObject, IPool where T : IPoolable { protected readonly Stack _available = new Stack(); public abstract IFactory Factory { get; set; } protected virtual T Create() { return Factory.Create(); } public virtual T Request() { T member = _available.Count > 0 ? _available.Pop() : Create(); member.OnRequest(); return member; } public virtual IEnumerable Request(int num = 1) { List members = new List(num); for (int i = 0; i < num; i++) { members.Add(Request()); } return members; } public virtual void Return(T member) { member.OnReturn(() => { _available.Push(member); }); } public virtual void Return(IEnumerable members) { foreach (T member in members) { Return(member); } } public virtual void OnDisable() { _available.Clear(); } } }