Skip to content

Object Pooling

Ciro Continisio edited this page Nov 18, 2020 · 7 revisions

Pool Diagram

Object pooling aims to alleviate the performance hit of instantiating/destroying many objects by instantiating them beforehand, and then activating/deactivating the objects instead, reusing objects when needed.

This is a common pattern in videogames even outside of Unity, but we have enriched it by using some Unity-specific features like ScriptableObjects as templates to setup pools directly in the Editor, without code.

Table of Contents

Creating a factory for the new type

using UnityEngine;
using UOP1.Factory;

[CreateAssetMenu(fileName = "MyComponentFactory", menuName = "Factory/MyComponent")]
public class MyComponentFactorySO : FactorySO<MyComponent>
{
    public override MyComponent Create(){
        return new GameObject("MyComponent").AddComponent<MyComponent>();
    }
}

Also, check out the page for the Factory implementation we have created for the game.

Creating a pool for the new type

using UnityEngine;
using UOP1.Pool;
using UOP1.Factory;

[CreateAssetMenu(fileName = "MyComponentPool", menuName = "Pool/MyComponent")]
public class MyComponentPoolSO : ComponentPoolSO<MyComponent>
{
	[SerializeField]
	private MyComponentFactorySO _factory;
	[SerializeField]
	private int _initialPoolSize;

	public override IFactory<MyComponent> Factory
	{
		get
		{
			return _factory;
		}
		set
		{
			_factory = value as MyComponentFactorySO;
		}
	}

	public override int InitialPoolSize
	{
		get
		{
			return _initialPoolSize;
		}
		set
		{
			_initialPoolSize = value;
		}
	}
}

Using the pool

There are two ways to use a pool. Either you can create the pool and factory as an asset in the project and set it to an object reference in the inspector or you can create the pool and factory at runtime.

As an asset:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyPoolManager : MonoBehaviour
{
	[SerializeField]
	private MyComponentPoolSO _pool = default;

	private void Start()
	{
		MyComponent myComponent = _pool.Request();
        //Do something with it
		_pool.Return(myComponent);
	}
}

At runtime:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MyRuntimePoolManager : MonoBehaviour
{
	[SerializeField]
	private int _initialPoolSize = 5;

	private MyComponentPoolSO _pool;

	private void Start()
	{
		_pool = ScriptableObject.CreateInstance<MyComponentPoolSO>();
		_pool.name = gameObject.name;
		_pool.Factory = ScriptableObject.CreateInstance<MyComponentFactorySO>();;
		_pool.InitialPoolSize = _initialPoolSize;
		MyComponent myComponent = _pool.Request();
		//Do something with it
		_pool.Return(myComponent);
	}

}

More info

To learn more about object pooling in general, check out the following links:

Home
Video materials

Basics

World building and Graphics

Game architecture

The game systems explained, with API examples. For programmers.

Game design

How-tos for designers to expand the game's gameplay.

  • Adding quests
  • Adding items
  • Creating dialogues
  • Making a cutscene
Clone this wiki locally