void loadInObjects(poolingStage stage) { foreach (objectPoolingInfo info in objectsToPool) { if (info.stageNeeded != stage) { continue; } //Making parent for this pool's objects GameObject Parent = new GameObject(); parentPairs[info.nameToCall] = Parent.transform; Parent.transform.SetParent(transform); Parent.name = info.nameToCall + " Pooling Parent"; //Registering the prefab in case it needs to be used requestObject() gameobjectPairs.Add(info.nameToCall, info.GO); //Making the pool itself and populating it queuePairs.Add(info.nameToCall, new Queue <GameObject>()); Queue <GameObject> poolingQueue = queuePairs[info.nameToCall]; for (int i = 0; i < info.initialNumber; i++) { GameObject newInstantiation = Instantiate(info.GO, Parent.transform); newInstantiation.SetActive(false); poolingQueue.Enqueue(newInstantiation); } } }
void loadOutObjects(poolingStage stage) { foreach (objectPoolingInfo info in objectsToPool) { if (info.stageNeeded != stage) { continue; } //Destroying all the gameobjects in the queue Queue <GameObject> objectQueue = queuePairs[info.nameToCall]; while (objectQueue.Peek() != null) { Destroy(objectQueue.Dequeue()); } } //It's not necessary to need to deal with the other dictoinaries. Yes, this is something like //memory leak, but it's not that significant. The most memory hungry dictionaty (the one with the pools themselves) have been dealt with. }
public void switchStage(poolingStage stage) { loadOutObjects(currentStage); currentStage = stage; loadInObjects(currentStage); }