patterncsharpMinor
Object Pooling in Unity
Viewed 0 times
objectunitypooling
Problem
I've made 2 object pooling classes -
The object to pool information is store in a
Which is later used in the 2 object pooling classes.
MonoObjectPooler : MonoBehaviour
```
///
/// Object pooler class which can hold a single type of objects.
///
public class MonoObjectPooler : MonoBehaviour
{
///
/// Object to be pooled.
///
public PooledObject PooledObject;
///
/// List to store all the objects that will be pooled.
///
private readonly List pooledObjects = new List();
private void Awake()
{
//Create the pooled object if the CreateOnAwake variable is set to true.
if (PooledObject.CreateOnAwake)
{
Populate();
}
}
///
/// Populates the list of pooled objects with PooledObjects.
///
public void Populate()
{
//Clear the previous items in the list.
pooledObjects.Clear();
//Load the items again
for (int i = 0; i
/// Returns a PooledObject.
///
public GameObject GetPooledObject()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
MonoObjectPooler : MonoBehaviour - can hold a single bucket of pooled objects of 1 type.PolyObjectPooler : MonoBehaviour - can hold multiple buckets of pooled objects of different types.The object to pool information is store in a
class PooledObject ///
/// Information holder for a pooled object
///
[Serializable]
public class PooledObject
{
[Tooltip(@"Name is used to differ the objects from one another")]
public string Name;
[Tooltip(@"What object should be created ?")]
public GameObject Object;
[Range(1, 10000)] [Tooltip(@"How much objects should be created ?")]
public int Amount;
[Tooltip(@"Can new objects be created in case there are none left ?")]
public bool CanGrow;
[Tooltip(@"False - objects must be created manually using Populate method
True - objects will be created automatically on awake")]
public bool CreateOnAwake;
}Which is later used in the 2 object pooling classes.
MonoObjectPooler : MonoBehaviour
```
///
/// Object pooler class which can hold a single type of objects.
///
public class MonoObjectPooler : MonoBehaviour
{
///
/// Object to be pooled.
///
public PooledObject PooledObject;
///
/// List to store all the objects that will be pooled.
///
private readonly List pooledObjects = new List();
private void Awake()
{
//Create the pooled object if the CreateOnAwake variable is set to true.
if (PooledObject.CreateOnAwake)
{
Populate();
}
}
///
/// Populates the list of pooled objects with PooledObjects.
///
public void Populate()
{
//Clear the previous items in the list.
pooledObjects.Clear();
//Load the items again
for (int i = 0; i
/// Returns a PooledObject.
///
public GameObject GetPooledObject()
{
for (int i = 0; i < pooledObjects.Count; i++)
{
Solution
I am not too familiar with Unity, so it's hard for me to comment on design. But I do have some experience with pools, so I want to point out, that depending on pool size this linear O(n) search:
can quickly become a serious performance bottleneck, that will negate any performance gains normally associated with pools. This problem is usually solved by actually removing "in-use" objects from the pool. And explicitly adding them back once pooled object is "released". This guaranties that any object that is present in the pool at any given time is not "in-use" so you can just "pop" the first one on every request (no search is needed). However, I do not know whether or not such design is applicable to Unity.
Also, both classes look like they can use a common ancestor and/or separate
for (int i = 0; i < pooledObjects.Count; i++)
{
if (!pooledObjects[i].activeInHierarchy)
{
//we have an available object
return pooledObjects[i];
}
}can quickly become a serious performance bottleneck, that will negate any performance gains normally associated with pools. This problem is usually solved by actually removing "in-use" objects from the pool. And explicitly adding them back once pooled object is "released". This guaranties that any object that is present in the pool at any given time is not "in-use" so you can just "pop" the first one on every request (no search is needed). However, I do not know whether or not such design is applicable to Unity.
Also, both classes look like they can use a common ancestor and/or separate
ObjectPool component to represent generic re-usable pool (SRP). As it stands, instantiation logic, population logic, search logic, etc. are basically copy-pasted.Code Snippets
for (int i = 0; i < pooledObjects.Count; i++)
{
if (!pooledObjects[i].activeInHierarchy)
{
//we have an available object
return pooledObjects[i];
}
}Context
StackExchange Code Review Q#150813, answer score: 4
Revisions (0)
No revisions yet.