I
I
i__egor2021-04-15 17:15:47
Unity
i__egor, 2021-04-15 17:15:47

What is the best way to make a pool of objects in a unit?

When you need to frequently spawn a lot of objects, instead of instantiate and destroy, it is better to enable, move, adjust and disable ready-made objects on the stage. Unity's official tutorial says:

public GameObject GetPooledObject()
{
    for(int i=0; i < amountToPool; i++)
    {
         if(!pooledObjects[i].activeInHierarchy)
         {
              return pooledObjects[i]
         }
    }
    return null;
}

That is, all objects are traversed in turn until an inactive one is found. That is, in order to take one object at a time in this way, you will have to sort through thousands of objects each time. And if there are still many such pools with different objects. Why not for example have two sheets instead of one, one with pending disabled objects, the other with running objects. If you need to get it, then we take one from the waiting ones, delete it there and add it to the active ones, if you need to remove it, on the contrary, remove it from the active list and add the waiting list

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question