classFish{
}var pool =newSimpleObjectPool<Fish>(()=>newFish(),initCount:50);
Debug.Log(pool.CurCount);// 50 var fish = pool.Allocate();
Debug.Log(pool.CurCount);// 49
pool.Recycle(fish);
Debug.Log(pool.CurCount);// 50// ---- GameObject ----var gameObjPool =newSimpleObjectPool<GameObject>(()=>{
var gameObj =newGameObject(""AGameObject"");// init gameObj code // gameObjPrefab = Resources.Load<GameObject>(""somePath/someGameObj"");return gameObj;},(gameObj)=>{
// reset code here});
SafeObjectPool 安全对象池
classBullet:IPoolable,IPoolType{
publicvoidOnRecycled(){
Debug.Log(""回收了"");}publicbool IsRecycled {
get;set;}publicstaticBulletAllocate(){
return SafeObjectPool<Bullet>.Instance.Allocate();}publicvoidRecycle2Cache(){
SafeObjectPool<Bullet>.Instance.Recycle(this);}}
SafeObjectPool<Bullet>.Instance.Init(50,25);var bullet = Bullet.Allocate();
Debug.Log(SafeObjectPool<Bullet>.Instance.CurCount);
bullet.Recycle2Cache();
Debug.Log(SafeObjectPool<Bullet>.Instance.CurCount);// can config object factory// 可以配置对象工厂
SafeObjectPool<Bullet>.Instance.SetFactoryMethod(()=>{
// bullet can be mono behaviourreturnnewBullet();});
SafeObjectPool<Bullet>.Instance.SetObjectFactory(newDefaultObjectFactory<Bullet>());// can set// 可以设置// NonPublicObjectFactory: 可以通过调用私有构造来创建对象,can call private constructor to create object// CustomObjectFactory: 自定义创建对象的方式,can create object by Func<T>// DefaultObjectFactory: 通过 new 创建对象, can create object by new
基本的数据结构封装 List、Dictionary
var names = ListPool<string>.Get()
names.Add(""Hello"");
names.Release2Pool();// or ListPool<string>.Release(names);
var infos = DictionaryPool<string,string>.Get()
infos.Add(""name"",""liangxie"");
infos.Release2Pool();// or DictionaryPool<string,string>.Release(names);