// Copyright (c) Pixel Crushers. All rights reserved. using UnityEngine; using System.Collections.Generic; namespace PixelCrushers { /// /// This generic class implements an object pool. It helps prevent garbage collection /// stutter by reusing objects without allocating and deallocating memory. /// public class Pool where T : new() { private List m_free = new List(); private List m_used = new List(); /// /// Gets an object from the pool. After getting an object, you should /// initialize its fields because it will have the values from its /// previous use. /// public T Get() { lock (m_free) { if (m_free.Count > 0) { T item = m_free[0]; m_used.Add(item); m_free.RemoveAt(0); return item; } else { T item = new T(); m_used.Add(item); return item; } } } /// /// Releases an object back to the pool. /// /// Item. public void Release(T item) { lock (m_free) { m_free.Add(item); m_used.Remove(item); } } /// /// Preallocates a number of objects into the pool. /// /// Initial size. public void Allocate(int initialSize) { while (m_free.Count < initialSize) { m_free.Add(new T()); } } /// /// Trims the pool to a maximum number of objects. /// /// Max objects. public void Trim(int max) { m_free.RemoveRange(0, Mathf.Min(m_free.Count, max)); } } }