Skip to content
Aprius edited this page Aug 28, 2024 · 16 revisions

Usages

How get object from pool

You can get object from pool via extension method Request

using Pancake;

[SerializeField] private GameObject prefab;

prefab.Request();

//request with set position
prefab.Request(Vector3.zero);

//request with return component
prefab.Request<Type Component>();

How return object to pool

You can return object to pool via extension method Return

using Pancake;

[SerializeField] private GameObject prefab;

var instance = prefab.Request();
instance.Return(); // return instance into pool

How clear pool

You can clear pool via extension method Clear

using Pancake;

[SerializeField] private GameObject prefab;

// If destroy active is true then even active instances will be destroyed
prefab.Clear(destroyActive: true);

Prewarm pool

You can prewarm pool with Populate method

using Pancake.Pools;
using UnityEngine;

public sealed class AudioManager : MonoBehaviour
{
    [SerializeField] private GameObject prefab;
    [SerializeField] private int prewarmSize = 10;
	
    private void Awake()
    {
        prefab.Prewarm(prewarmSize);
    }
}

Use callbacks

When using the pool, we have two callbacks: callback when an object is removed from the pool and callback when an object is returned to the pool.

  • OnRequest : callback when an object is take out the pool
  • OnReturn : callback when an object is put back the pool

To implement these two callbacks we need to implement the IPoolCallbackReceiver interface

using Pancake.Pools;
using UnityEngine;

public class Enemy: MonoBehaviour, IPoolCallbackReceiver
{
	[SerializeField] private float maxHP = 100f;
	
	private float _currentHP = 0f;
	
	/// <summary>
	/// This method will be called before Awake
	/// </summary>
	public void OnRequest()
	{
		_currentHP = maxHP;
	}
		
	public void OnReturn()
	{
		// TO_DO
	}
}
Clone this wiki locally