using System; using System.Collections.Generic; using Unity.Collections; using UnityEngine; namespace AwesomeTechnologies.Utility { [Serializable] public class MatrixListPool { public List> PoolList = new List>(); public int MaxCapasity; public int CreateCount; private List _returnList; public MatrixListPool(int poolCount, int capasity) { CreateCount = 0; MaxCapasity = capasity; for (int i = 0; i <= poolCount - 1; i++) CreateList(); } private void CreateList() { CreateCount++; List newList = new List(MaxCapasity); PoolList.Add(newList); } public List GetList() { if (PoolList.Count == 0) CreateList(); _returnList = PoolList[PoolList.Count - 1]; PoolList.RemoveAt(PoolList.Count - 1); return _returnList; } public void ReturnList(List list) { if (list.Capacity > MaxCapasity) MaxCapasity = list.Capacity; list.Clear(); PoolList.Add(list); } } [Serializable] public class ListPool { public List> PoolList = new List>(); public int MaxCapasity; public int CreateCount; private List _returnList; public ListPool(int poolCount, int capasity) { CreateCount = 0; MaxCapasity = capasity; for (int i = 0; i <= poolCount - 1; i++) CreateList(); } private void CreateList() { CreateCount++; List newList = new List(MaxCapasity); PoolList.Add(newList); } public List GetList() { if (PoolList.Count == 0) CreateList(); _returnList = PoolList[PoolList.Count - 1]; PoolList.RemoveAt(PoolList.Count - 1); return _returnList; } public void ReturnList(List list) { if (list.Capacity > MaxCapasity) MaxCapasity = list.Capacity; list.Clear(); PoolList.Add(list); } } [Serializable] public class ObjectPool where T : new() { private readonly List _available = new List(); private readonly List _inUse = new List(); public T Get() { lock (_available) { if (_available.Count != 0) { T obj = _available[0]; _inUse.Add(obj); _available.RemoveAt(0); return obj; } else { T obj = new T(); _inUse.Add(obj); return obj; } } } public void Release(T obj) { CleanUp(obj); lock (_available) { _available.Add(obj); _inUse.Remove(obj); } } private void CleanUp(T obj) { } } [Serializable] public class RaycastHitListPool { private readonly List> _available = new List>(); public NativeList Get() { lock (_available) { if (_available.Count != 0) { NativeList obj = _available[0]; _available.RemoveAt(0); return obj; } else { NativeList obj = new NativeList(0,Allocator.Persistent); return obj; } } } public void Return(NativeList obj) { CleanUp(obj); lock (_available) { _available.Add(obj); } } private void CleanUp(NativeList obj) { obj.Clear(); } public void Dispose() { for (int i = 0; i <= _available.Count - 1; i++) { if (_available[i].IsCreated) _available[i].Dispose(); } lock (_available) { _available.Clear(); } } } [Serializable] public class RaycastCommandListPool { private readonly List> _available = new List>(); public NativeList Get() { lock (_available) { if (_available.Count != 0) { NativeList obj = _available[0]; _available.RemoveAt(0); return obj; } else { NativeList obj = new NativeList(0, Allocator.Persistent); return obj; } } } public void Return(NativeList obj) { CleanUp(obj); lock (_available) { _available.Add(obj); } } private void CleanUp(NativeList obj) { obj.Clear(); } public void Dispose() { for (int i = 0; i <= _available.Count - 1; i++) { if (_available[i].IsCreated) _available[i].Dispose(); } lock (_available) { _available.Clear(); } } } }