Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 60 additions & 39 deletions PoolManagement/PoolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ public class PoolManager<T> where T : MonoBehaviour, IPoolItem
private Transform _container;
private bool _autoReuse;

private T[] _prefabs;
private int[] _prefabsShuffleBag;
private bool _autoResize;
private int _poolSize;

#region Contstructors
public PoolManager(Transform container, int size, bool autoReuse, T[] prefabs, params int[] prefabsShuffleBag)
public PoolManager(Transform container, int size, bool autoReuse, bool autoResize,T[] prefabs, params int[] prefabsShuffleBag)
{
_autoReuse = autoReuse;
_autoResize = autoResize;
_poolSize = size;
_prefabs = prefabs;
_prefabsShuffleBag = prefabsShuffleBag;

_pool = new List<T>();
_active = new List<T>();

Expand All @@ -27,50 +37,22 @@ public PoolManager(Transform container, int size, bool autoReuse, T[] prefabs, p
_pool.Add(child);
}

if (_pool.Count < size && prefabs != null && prefabs.Length > 0 && prefabs[0] != null)
{
ShuffleBag<int> bag = new ShuffleBag<int>();
for (int i = 0; i < prefabs.Length; i++)
{
bag.Add(i, prefabsShuffleBag.Length > i ? prefabsShuffleBag[i] : 1);
}

int attempts = size + 200;
while (_pool.Count < size && attempts > 0)
{
T prefab = prefabs[bag.Next()];
if (prefab != null)
{
T newPoolObject = GameObject.Instantiate(prefabs[bag.Next()]) as T;
if (newPoolObject != null)
{
newPoolObject.transform.SetParent(container);
newPoolObject.Init();
_pool.Add(newPoolObject);
}
else
attempts--;
}
else
attempts--;
}

bag.Clear();
bag = null;
}

prefabs = null;
}

public PoolManager(Transform container, int size, bool autoReuse, string[] prefabs, params int[] prefabsShuffleBag) : this(container, size, autoReuse, ConvertPathsToPrefabs(prefabs), prefabsShuffleBag) {}
public PoolManager(Transform container, int size, bool autoReuse) : this(container, size, autoReuse, new string[0]) {}
public PoolManager(Transform container, int size, bool autoReuse, bool autoResize, string[] prefabs, params int[] prefabsShuffleBag) : this(container, size, autoReuse,autoResize, ConvertPathsToPrefabs(prefabs), prefabsShuffleBag) {}
public PoolManager(Transform container, int size, bool autoReuse, string[] prefabs, params int[] prefabsShuffleBag) : this(container, size, autoReuse,false, ConvertPathsToPrefabs(prefabs), prefabsShuffleBag) {}

public PoolManager(Transform parent, string containerName, int size, bool autoReuse, T[] prefabs, params int[] prefabsShuffleBag) : this(CreateContainer(parent, containerName), size, autoReuse, prefabs, prefabsShuffleBag) {}
public PoolManager(Transform parent, string containerName, int size, bool autoReuse, string[] prefabs, params int[] prefabsShuffleBag) : this(CreateContainer(parent, containerName), size, autoReuse, prefabs, prefabsShuffleBag) {}
public PoolManager(T prefab, Transform container, int size, bool autoReuse) : this(container, size, autoReuse, new T[] { prefab }, 1) { }
public PoolManager(string prefab, Transform container, int size, bool autoReuse) : this(Resources.Load<T>(prefab), container, size, autoReuse) { }
public PoolManager(T prefab, Transform parent, string containerName, int size, bool autoReuse) : this(prefab, CreateContainer(parent, containerName), size, autoReuse) { }
public PoolManager(string prefab, Transform parent, string containerName, int size, bool autoReuse) : this(prefab, CreateContainer(parent, containerName), size, autoReuse) { }
public PoolManager(Transform container, int size, bool autoReuse,bool autoResize = false) : this(container, size, autoReuse,autoResize, new string[0]) {}

public PoolManager(Transform parent, string containerName, int size, bool autoReuse,bool autoResize, T[] prefabs, params int[] prefabsShuffleBag) : this(CreateContainer(parent, containerName), size, autoReuse,autoResize, prefabs, prefabsShuffleBag) {}
public PoolManager(Transform parent, string containerName, int size, bool autoReuse,bool autoResize, string[] prefabs, params int[] prefabsShuffleBag) : this(CreateContainer(parent, containerName), size, autoReuse,autoResize, prefabs, prefabsShuffleBag) {}
public PoolManager(T prefab, Transform container, int size, bool autoReuse, bool autoResize = false) : this(container, size, autoReuse, autoResize, new T[] { prefab }, 1) { }
public PoolManager(string prefab, Transform container, int size, bool autoReuse,bool autoResize = false) : this(Resources.Load<T>(prefab), container, size, autoReuse,autoResize) { }
public PoolManager(T prefab, Transform parent, string containerName, int size, bool autoReuse,bool autoResize = false) : this(prefab, CreateContainer(parent, containerName), size, autoReuse,autoResize) { }
public PoolManager(string prefab, Transform parent, string containerName, int size, bool autoReuse,bool autoResize = false) : this(prefab, CreateContainer(parent, containerName), size, autoReuse,autoResize) { }
#endregion

#region Constructor Helpers
Expand Down Expand Up @@ -113,6 +95,10 @@ public virtual void ReturnInactiveItemsToPool(System.Action<T> returnToPoolCallb

public virtual T GetItemFromPool()
{
if (_pool.Count == 0 && _autoResize)
{
FillPool();
}
if (_pool.Count > 0)
{
T item = _pool[0];
Expand Down Expand Up @@ -167,5 +153,40 @@ public List<T> Active
{
get { return _active; }
}

private void FillPool()
{
if (_pool.Count < _poolSize && _prefabs != null && _prefabs.Length > 0 && _prefabs[0] != null)
{
ShuffleBag<int> bag = new ShuffleBag<int>();
for (int i = 0; i < _prefabs.Length; i++)
{
bag.Add(i, _prefabsShuffleBag.Length > i ? _prefabsShuffleBag[i] : 1);
}

int attempts = _poolSize - _pool.Count + 200;
while (_pool.Count < _poolSize && attempts > 0)
{
T prefab = _prefabs[bag.Next()];
if (prefab != null)
{
T newPoolObject = GameObject.Instantiate(_prefabs[bag.Next()]) as T;
if (newPoolObject != null)
{
newPoolObject.transform.SetParent(_container);
newPoolObject.Init();
_pool.Add(newPoolObject);
}
else
attempts--;
}
else
attempts--;
}

bag.Clear();
bag = null;
}
}
}
}