com.amanotes.prefabpool
v1.0.1
Published
Generic object pooling system for Unity
Maintainers
Readme
Prefab Pool
Generic object pooling system for Unity.
Features
- ✅ Simple API for Get/Return pattern
- ✅ Automatic prefab instantiation
- ✅
IPoolPrefabinterface for lifecycle callbacks - ✅ Pre-warming support
- ✅ Material pooling support
- ✅ No dependencies on other packages
Installation
Add to your manifest.json:
{
"dependencies": {
"com.amanotes.prefabpool": "1.0.0"
}
}Quick Start
using Amanotes.PrefabPool;
public class Example : MonoBehaviour
{
void Start()
{
var pool = new PrefabPool();
var group = CreatePoolGroup();
pool.RegisterGroup(group, transform);
// Get object from pool
var obj = pool.Get<SpriteRenderer>("enemy");
// Return to pool when done
pool.Return(obj.gameObject);
}
}Usage
Implement IPoolPrefab
public class Enemy : MonoBehaviour, IPoolPrefab
{
public void OnGet()
{
// Called when retrieved from pool
gameObject.SetActive(true);
ResetState();
}
public void OnReturn()
{
// Called when returned to pool
gameObject.SetActive(false);
}
}Create Pool Configuration
var poolGroup = ScriptableObject.CreateInstance<PoolGroup>();
poolGroup.name = "Enemies";
poolGroup.entries = new List<PoolEntry>
{
new PoolEntry
{
id = "enemy",
prefab = enemyPrefab,
prewarmCount = 10
}
};API Reference
IPrefabPool Interface
void RegisterGroup(PoolGroup group, Transform parent);
T Get<T>(string type) where T : Component;
void Return(GameObject obj);
IEnumerator Initialize();IPoolPrefab Interface
void OnGet(); // Called when retrieved
void OnReturn(); // Called when returnedLicense
Copyright © Amanotes
