/// <summary> /// Retrieves an object from the top of the pool /// </summary> public T Pop() { if (m_objectIndex >= m_objects.Count) { // Capacity limit has been reached, allocate new elemets m_objects.Add(m_objectAllocator.Action(m_objectAllocator.Arg)); // Let Unity handle how much memory is going to be preallocated for (int i = m_objects.Count; i < m_objects.Capacity; i++) { m_objects.Add(m_objectAllocator.Action(m_objectAllocator.Arg)); } } return(m_objects[m_objectIndex++]); }
public ObjectPool(ObjectPoolAllocator <T> objectAllocator, Action <T> objectDeallocator, int initialSize) { m_objectAllocator = objectAllocator; m_objectDeallocator = objectDeallocator; m_initialSize = initialSize; m_autoReleaseMemory = true; m_objectIndex = 0; m_objects = new List <T>(initialSize); for (int i = 0; i < initialSize; i++) { m_objects.Add(m_objectAllocator.Action(m_objectAllocator.Arg)); } }
public ObjectPool(Func <T, T> objectAllocator, int initialSize, bool autoReleaseMememory) { m_objectAllocator = new ObjectPoolAllocator <T>(objectAllocator); m_objectDeallocator = null; m_initialSize = initialSize; m_autoReleaseMemory = autoReleaseMememory; m_objectIndex = 0; m_objects = new List <T>(initialSize); for (int i = 0; i < initialSize; i++) { m_objects.Add(m_objectAllocator.Action(m_objectAllocator.Arg)); } }