Pool

Unity Pool utilities

In One Line

Where objects never die, they just take a break.

Overview

Pool is a resource control system. It will keep objects in memory to become ready to use.

Please read wiki page of Pool for details.

Package Info

display name

AceLand Pool

package name

com.aceland.pool

latest version

1.0.4

namespace

AceLand.Pool

dependencies

com.aceland.library: 1.0.4

Why Use It

Unity Engine has it's own pool system. However you need to set everything, what to do on take from or return to pool, including instantiate and destroy.

This package has already created all necessary code for pool running in official method. And additionally add some management settings on it.

  • On Create

    • Instantiate OwnedItemPrefab

  • On Get

    • Invoke PoolItem.OnTakeFromPool()

    • Parent to Target Parent

    • Enable the game object

  • On Return

    • Invoke PoolItem.OnReturnToPool()

    • Disable the game object

    • Parent to Pool Parent

  • On Destroy

    • Destory the game object

Please read the Unity official document about Object Pool.


Pool Item

using AceLand.Pool;
using UnityEngine;

public class YourPoolObject : MonoBehaviour, IPoolItem
{
    public void OnTakeFromPool()
    {
        Debug.Log("Take from pool");
    }

    public void OnReturnToPool()
    {
        Debug.Log("Return to pool");
    }
}
  • Create your Pool Item as an MonoBehaviour

  • Attach the component to GameObject and make it as a prefab


Pool Settings

using UnityEngine;

public class YourComponent : MonoBehaviour
{
    [SerializeField] private PoolSettings<YourPoolObject> poolSettings;
}
  • kindly add a line, you can setup your pool now

Owned Item Prefab

IPoolItem that made as prefab

Pool Parent

Transform that hold objects not taken out

Target Parent

Transform That hold objects taken out

Pool Type

Stack or LinkedList, read Pool Type section for details

Prewarm Size

start and min size of the pool

Max Size

max size of the pool

Collection Checks

check that the objects being returned to the pool are the same type as those that were originally pooled. this may cause performance issue.


Pool Type

  • Small Pool:

    • Stack: More efficient in terms of memory and performance for small sizes; simple to implement.

    • Linked List: May incur unnecessary overhead; less common unless specific behavior is required.

  • Large Pool:

    • Stack: Efficient but may require careful management of memory and resizing. Suitable if LIFO access is necessary.

    • Linked List: More flexible for dynamic sizes; better for frequent insertions/deletions, but with higher memory overhead.


Create the Pool

using AceLand.Pool;
using UnityEngine;

public class YourComponent : MonoBehaviour
{
    [SerializeField] private PoolSettings<YourPoolObject> poolSettings;

    private Pool<YourPoolObject> _pool;

    private void Awake()
    {
        _pool = Pool<YourPoolObject>.Builder()
            .WithSettings(poolSettings)
            .WithPoolParent(transform)    // optional
            .WithTargetParent(transform)  // optional
            .Build();
    }
    
    private void OnDestroy()
    {
        _pool?.ReleaseAll();
        _pool?.Clear();
    }
}

Pool<T> where T : MonoBehaviour, IPoolItem


Use the Pool

// Get the Asset name in Pool
string itemName = _pool.ItemName;

// Get object from pool
YourPoolObject item = _pool.Get();

// return object to pool
_pool.Release(item);

// return all pooled object
_pool.ReleaseAll();

// return a random pooled object
_pool.ReleaseRandom();

// all pooled objects
IEnumerable<YourPoolObject> items = _pool.PooledItems;

// get number of pooled objects
int pooledCount = _pool.CountPooled;

// get number of inactive objects
int pooledInactive = _pool.CountInactive;

// Clear and remove all pool objects
_pool.Clear();

Last updated