/// <summary> /// Retrieves an object from the top of the pool /// </summary> public T Pop() { if (objectIndex >= objects.Count) { // Capacity limit has been reached, allocate new elemets objects.Add(objectAllocator.action(objectAllocator.arg)); // Let Unity handle how much memory is going to be preallocated for (int i = objects.Count; i < objects.Capacity; i++) { objects.Add(objectAllocator.action(objectAllocator.arg)); } } return(objects[objectIndex++]); }
public ObjectPool(Func <T, T> allocator, int initialSize, bool autoReleaseMememory) { objectAllocator = new ObjectPoolAllocator <T>(allocator); objectDeallocator = null; this.initialSize = initialSize; autoReleaseMemory = autoReleaseMememory; objectIndex = 0; objects = new List <T>(initialSize); for (int i = 0; i < initialSize; i++) { objects.Add(objectAllocator.action(objectAllocator.arg)); } }