AceLand Unity Packages
  • Home
  • Getting Started
    • Installation
    • Project Settings
    • Architecture Graph
    • Development Level
  • Tutorial
    • Create Your Package
    • Create Project Settings
  • Packages
    • Library
      • Change Log
      • Editor Tools
      • Mono
        • Follow Object
        • Singleton
      • Attributes
        • Conditional Show
        • Inspector Button
        • ReadOnly Field
      • Build Leveling
      • CVS
      • DataTools
      • Disposable Object
      • Extensions
      • Json
      • Kalman Filter
      • Optional
      • Project Setting
      • Serialization Surrogate
      • Utils
    • Event Driven
      • Change Log
      • Event Bus
      • Signal
    • Input
      • Change Log
    • Memento Service
      • Change Log
    • Node Framework
      • Change Log
      • Mono Node
    • Node For Mono (dev)
    • Player Loop Hack
      • Change Log
    • Pool
      • Change Log
    • Security (dev)
    • States
      • Change Log
    • Task Utils
      • Change Log
    • WebRequest
      • Change Log
Powered by GitBook
On this page
  • In One Line
  • Overview
  • Package Info
  • Why Use It
  • Pool Item
  • Pool Settings
  • Pool Type
  • Create the Pool
  • Use the Pool
  1. Packages

Pool

Unity Pool utilities

Last updated 2 months ago

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 for details.

Package Info

display name

AceLand Pool

package name

latest version

1.0.4

namespace

git repository

dependencies

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


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();

Please read the about Object Pool.

com.aceland.pool
AceLand.Pool
com.aceland.library: 1.0.4
wiki page of Pool
Unity official document
https://github.com/parsue/com.aceland.pool.git