52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BF
|
|
{
|
|
public class PoolManager : ManagerBase
|
|
{
|
|
static PoolManager instance;
|
|
public static PoolManager Create()
|
|
{
|
|
BFLog.LogAssert(instance == null, "This method only allows BFMain to call once");
|
|
instance = new PoolManager();
|
|
instance.Init();
|
|
return instance;
|
|
}
|
|
|
|
private PoolManager() { }
|
|
|
|
public override void Init()
|
|
{
|
|
base.Init();
|
|
GameObjPool = new GameObjectPool();
|
|
}
|
|
|
|
public override void Destroy()
|
|
{
|
|
foreach (var item in classPoolDict)
|
|
{
|
|
classPoolDict[item.Key].Clear();
|
|
}
|
|
classPoolDict.Clear();
|
|
instance = null;
|
|
}
|
|
|
|
Dictionary<Type, IObjectPool> classPoolDict = new Dictionary<Type, IObjectPool>();
|
|
public GameObjectPool GameObjPool { get; private set; }
|
|
|
|
public ClassPool<T> GetClassPool<T>(int size = 100) where T : class
|
|
{
|
|
var t = typeof(T);
|
|
classPoolDict.TryGetValue(t, out IObjectPool pool);
|
|
if (pool == null)
|
|
{
|
|
var newPool = ClassPool<T>.Create(size);
|
|
classPoolDict.Add(t, newPool);
|
|
return newPool;
|
|
}
|
|
return pool as ClassPool<T>;
|
|
}
|
|
}
|
|
}
|