Skip to content
yenmoc edited this page Dec 25, 2022 · 16 revisions

Pre-caching

If you want to prewarm the pool before use, you can create using PoolPreset

Create pool preset via menu :

Create > Pancake > Create Pool Preset

Screenshot_2

For example, the cube preset below has two elements, Cube and Sphere .

Screenshot_1

Each PoolPreset element both have two properties, Prefab and Size

Parameters Info
Prefab Object to spawn
Size Spawn count

Then you can then use PoolEntry to initialize the pool preset. Add component PoolEntry on any GameObject in scene and assign the pool preset you want to setup

image

Usages

It's pretty simple to use everything you need is to call the Spawn or Despawn method from the MagicPool class.

    public GameObject prefab;

    private GameObject _spawnedObject;
    
    public void Spawn()
    {
        _spawnedObject = MagicPool.Spawn(prefab);
    }

    public void Despawn()
    {
        MagicPool.Despawn(_spawnedObject);
    }

In addition to these two methods, it also has a number of other support methods such as OnDespawned, OnDespawned, DestroyPool, DestroyAllPools and Reset

Method or Event Info
Action<GameObject> OnSpawned This Action called on any object spawned
Action<GameObject> OnDespawned This Action called on any object despawned
InstallPoolPreset() Pre-caches game objects by PoolPreset
DestroyPool(gameObject) Destroys pool by GameObject or Pool
DestroyAllPools() Destroys all pools
GetPoolByPrefab() Returns Pool by prefab
Reset() Resets the MagicPool. Called in PoolEntry in OnDestroy

IPoolable

If you want to invoke methods OnSpawn() and OnDespawn() on poolable GameObject, implement interface IPoolable on GameObject

    public class Unit : MonoBehaviour, IPoolable
    {
        void IPoolItem.OnSpawn()
        {
            Debug.Log("Unit Spawned");
        }
        
        void IPoolItem.OnDespawn()
        {
            Debug.Log("Unit Despawned");
        }
    }

PoolDespawner

You can add component PoolDespawner on any prefab that will be spawned and must be despawned after a certain amount of time. This time is set in the field time.

Poolable

Poolable added to any object created using the MagicPool

Property Info
Pool Pool of the current poolable object
Prefab Prefab of the current poolable object
IsActive Means the object is enabled or disabled

Pool

Pool creates for each prefab that was used to spawn object

Property or Method Info
Prefab Prefab of the current pool
Parent Parent transform for each spawned game object of the current pool
Poolables All spawned poolables of the current Pool
Get() Return unused poolable object
Populate() Populate pool by spawn new poolable objects
Add() Add new poolable objects in pool
Remove() Remove existing spawned objects from pool
Clone this wiki locally