English | Русский
Reuse objects without allocations.
A lightweight generic object pool for Unity and pure C# projects. Provides Pool<T> for a single type and MultiPool<TKey, TValue> for managing multiple pools under a common key.
- .unitypackage — Releases
- UPM —
Window → Package Manager→+→Add package from git URL:https://github.com/SST-Systems/Pooling.gitAppend#tagto pin a version. - Manual — clone or download, copy to
Assets/.
Unity 2021.3+
Pool<T> — a single typed pool.
MultiPool<TKey, TValue> — a dictionary of pools keyed by any type (typically System.Type). Lets you manage many pools through a single entry point.
var pool = new Pool<MyObject>(
factory: () => new MyObject(),
actionOnGet: obj => obj.Reset(),
actionOnRelease: obj => obj.Cleanup()
);
// Prewarm — create instances ahead of time
pool.Prewarm(10);
// Get an instance (creates a new one if the pool is empty)
var obj = pool.Get();
// Return it
pool.Release(obj);
// Permanently remove an instance without returning it
pool.Discard(obj);
// Clear all instances
pool.DiscardAll();var multiPool = new MultiPool<Type, IHandler>();
// Register a factory for a specific key
multiPool.RegisterFactory(typeof(MyHandler), () => new MyHandler());
// Get and release by key
var handler = multiPool.Get(typeof(MyHandler));
multiPool.Release(typeof(MyHandler), handler);Pool<T>tracks both free and occupied instances — double-releasing an instance is silently ignored.MultiPoolthrowsKeyNotFoundExceptionif you callGetbefore registering a factory for that key.
Distributed under the MIT License. Free for personal and commercial use.
Author — Egor Shesterikov.
