c1_unity/Assets/Scripts/Common/Pool/PoolManager.cs
2023-04-03 11:04:31 +08:00

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>;
}
}
}