- Home /
When should I use object pool?
I am doing a 2D game that generate at every second a game object. And the game object is different every time, for that I am using like 12 prefabs, but 9 of then are pretty the same, the only difference is the sprite. So there are 3 very different prefabs with different children, diffrent scripts , and different polygon collider and size. So to do a object pool for them I need to change the size , the sprite , the child and the polygon collider. for that and need every time to destroy some components and adding them.
So should I use the object pooling or just instantiate and destroy them?
You mention this:
"So to do a object pool for them I need to change the size , the sprite , the child and the polygon collider. for that and need every time to destroy some components and adding them."
However, depending on your game needs you may not want to do that at all. If there is not a ton of randomization, you could simply pool a dozen of those objects, knowing you will only ever have x amount at a time, and reuse those objects over and over. Pooling does not necessarily mean you need to have a handful of objects that are constantly manipulated, you could ins$$anonymous$$d have more objects that remain the same. It really comes down to your game and where you have the resources to spend.
With that being said, if you are looking to hit a high FPS on lower end mobile, object pooling is almost a requirement at this point. Lucky for you, there are some good resources out there for learning about object pooling and a slew of pooling systems on the asset store you can look in at well, even some free ones!
Answer by Pan_Migo · Mar 30, 2017 at 02:03 PM
Well because you want to generate every second a game object, object pooling is far better in terms of performance (most specifically CPU and memory), than instantiating and destroying. Also, I assume that because you make a 2d game you may want to target for web or mobile, where the community widely suggests using object pooling.
So even in case, where pooling objects concerning performance may not have too much difference in your game I highly suggest you learn how to do it because some day you might want to use it. I recently created a generic object pooling class based on the unity tutorials, which you can use for any object you want :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPooler : MonoBehaviour {
public static ObjectPooler current;
public GameObject pooledObject;
public int pooledAmount = 11;
//list used for object pooling
List<GameObject> pooledObjects;
//reference this game object
void Awake() {
current = this;
}
// Use this for initialization
void Start () {
pooledObjects = new List<GameObject>();
for(int i =0; i < pooledAmount; i++){
GameObject obj = (GameObject) Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
}
}
public GameObject GetPooledObject(){
for(int i=0; i < pooledObjects.Count ; i++)
{
if(!pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
return null;
}
}
Answer by Rhombuster · Mar 29, 2017 at 06:00 PM
This is more of a judgement call. It depends on the complexity of your code, the frequency you're creating objects, the platform you're targeting, so I can't say you definitively should or shouldn't based on the information you've given. However, there is a resource I use that is free and has some guidelines. I'd suggest reading it: http://gameprogrammingpatterns.com/object-pool.html
Here is a snippet about when to use object pools:
This pattern is used widely in games for obvious things like game entities and visual effects, but it is also used for less visible data structures such as currently playing sounds. Use Object Pool when:
You need to frequently create and destroy objects.
Objects are similar in size.
Allocating objects on the heap is slow or could lead to memory fragmentation.
Each object encapsulates a resource such as a database or network connection that is expensive to acquire and could be reused.