Table of Contents

Self-Contained MonoBehaviour Pool

Runtime/UnityObject/PoolableBehaviour.cs   HeaderDoc

(c) 2024 Sator Imaging, Licensed under the MIT License https://github.com/sator-imaging/Unity-Fundamentals

Singly linked list based GameObject & MonoBehaviour pooling.

HOW TO USE

// inherit PoolableBehaviour<TSelf>
public class MyPooledBehaviour: PoolableBehaviour<MyPooledBehaviour>
{
    // your code here
}

// new GameObject will be created for your component if pool is empty
var component = MyPooledBehaviour.Rent(activateGameObject: false, parent: null);

// to return instance to pool
component.ReturnToPool();
// --- or ---
component.ReturnToPoolIfCanceled(otherComponent.destroyCancellationToken);

// populate instances (aka. warmup)
MyPooledBehaviour.Populate(count: 10);

// purge unused instances from pool
MyPooledBehaviour.TrimExcess(maxInstanceCountInPool: 3);

// there is a helper function can be called in inheritance method
ThrowIfAlreadyReturnedToPool("error message");