publicclassPoolTest:Test{publicinterfaceIObject{}publicclassObj_A:IObject{}publicclassObj_B:IObject{}privatevoidFastExample(){
Log.L("Create a auto create pool");ActivatorCreatePool<Obj_A> pool =newActivatorCreatePool<Obj_A>();
Log.L("Get an instance from pool");var _obj = pool.Get();
Log.L($"the type of instance is {_obj.GetType()}");
Log.L("Let's put the instance to pool");
pool.Set(_obj);}privateclassMyPool:ObjectPool<Obj_A>{protectedoverrideObj_ACreatNew(IEventArgs arg){returnnewObj_A();}}privatevoidNormalTest(){
Log.L("Create a auto create pool");MyPool pool =newMyPool();
Log.L("Get an instance from pool");var _obj = pool.Get();
Log.L($"the type of instance is {_obj.GetType()}");
Log.L("Let's put the instance to pool");
pool.Set(_obj);}privateclassMutiPool:BaseTypePool<IObject>{}privatevoidMutiTest(){
Log.L("Create a auto create pool");MutiPool pool =newMutiPool();
Log.L("Get an instance A from pool");IObject _obj = pool.Get<Obj_A>();
Log.L($"the type of instance is {_obj.GetType()}");
Log.L("Let's put the instance to pool");
pool.Set(_obj);
Log.L("Get an instance B from pool");
_obj = pool.Get<Obj_B>();
Log.L($"the type of instance is {_obj.GetType()}");
Log.L("Let's put the instance to pool");
pool.Set(_obj);}protectedoverridevoidStart(){
Log.L("--------------------------");FastExample();
Log.L("--------------------------");NormalTest();
Log.L("--------------------------");MutiTest();}protectedoverridevoidStop(){}protectedoverridevoidUpdate(){}}
在开头相信看到对象池,大家是很熟悉了就算你不熟悉,如果你在CSDN 搜索的话,我相信可以搜索到 一堆开手枪的 unity版本对象池那么IF的有什么区别?IF主要提供了三种对象池1、自动实例化的对象池2、自己个性化管理生命周期的对象池3、基类对象池,这是啥?简单讲解:声明一个动物的对象池,你可以取出猫,同时也可以取出狗上马,上马 public class PoolTest : Test { public interface IObject { }