The Unity object pooling can load and cache all objects inherited from UnityEngine.Object, and can easily expand the loading method, such as loading objects from AssetBundle. And contains CommonPool to cache other type objects.
ObjectPoolcan load and cache all objects inherited fromUnityEngine.Object.- For
CommonPool, build in ListPool, QueuePool, StackPool, HashSetPool, DictionaryPool to use. - Unused assets/objects and
PoolItemwill auto destroy byDeleteTime. IfDeleteTimeis -1, they will not auto destroy. LoadMode.Resourceto cache from Resources folder.LoadMode.Customto cache from anywhere by callObjectPool.Instance.RegisterCustomPoolItemfunction first.- If you cache
GameObject, you can implementISpawnHandlerandIDisposeHandlerinterfaces to listen spawn and dispose actions.
public interface ISpawnHandler
{
void OnSpawn();
}
public interface IDisposeHandler
{
void OnDispose();
}Or you can add listener from ObjectPoolItemKey.SpawnEvent and ObjectPoolItemKey.DisposeEvent.
public event Action<ObjectPoolItemKey> SpawnEvent;
public event Action<ObjectPoolItemKey> DisposeEvent;- Basically does not consume extra memory and GC.
- If you want to cache Object from
AssetBundle, maybe you can use AssetBundleManager(Open source) to manage your AssetBundles.
public ObjectPoolItem(Type assetType, ObjectPool.LoadMode loadMode, string bundleName, string assetName, DeleteTime deleteTime) : base(deleteTime)
{
...
switch (mLoadMode)
{
case ObjectPool.LoadMode.Resource:
OriginAsset = Resources.Load(assetName, assetType);
break;
case ObjectPool.LoadMode.Custom:
break;
// MARK: Use AssetBundleManager to load asset.
case ObjectPool.LoadMode.AssetBundle:
OriginAsset = AssetBundleManager.GetAsset(assetType, bundleName, assetName);
break;
// MARK: Add yourself load methods
}
...
}