using System;
using UnityEngine;
namespace TyphoonPool
{
///
/// 池管理器
///
public static class PoolManager
{
#if UNITY_EDITOR
[UnityEditor.InitializeOnEnterPlayMode]
private static void OnEnterPlayMode()
{
_impl = null;
}
#endif
private static PoolManageImpl _impl = null;
private static PoolManageImpl Impl
{
get
{
if (_impl == null)
{
_impl = new GameObject("PoolManager").AddComponent();
GameObject.DontDestroyOnLoad(_impl.gameObject);
}
return _impl;
}
}
//添加池
internal static void AddPool(Pool pool) => Impl.AddPool(pool);
//移除池
internal static void RemovePool(ulong poolID) => Impl.RemovePool(poolID);
///
/// 尝试获取池
///
/// 池ID
/// 匹配的结果
///
public static bool TryGetPool(ulong poolID, out Pool match) => Impl.TryGetPool(poolID, out match);
///
/// 是否存在池
///
/// 池ID
///
public static bool ContainsPool(ulong poolID) => Impl.ContainsPool(poolID);
///
/// 获取池,如果找不到会报错
///
/// 池ID
///
public static Pool GetPool(ulong poolID) => Impl.GetPool(poolID);
///
/// 获取池,如果找不到返回默认值null
///
/// 池ID
///
public static Pool GetPoolOrDefault(ulong poolID) => Impl.GetPoolOrDefault(poolID);
///
/// 回收池对象
///
/// 池对象实例
public static void Recycle(IPoolObject poolObject) => Impl.Recycle(poolObject);
///
/// 回收池对象
///
/// 池ID
/// 池对象
public static void Recycle(ulong poolID, ulong objectID) => Impl.Recycle(poolID, objectID);
///
/// 推荐使用Recycle(PoolObject poolObject)进行回收,效率最快
/// 此API会进行遍历,找到匹配的池进行回收
///
public static void Recycle(ulong poolObject) => Impl.Recycle(poolObject);
///
/// 创建对象池
///
/// 池对象创建逻辑
/// 池子名
/// 池是否永不销毁
///
public static Pool CreatePool(Func func, string poolName = null, bool neverDestroyInLoad = false)
=> Pool.Internal_CreatePool(func, poolName, neverDestroyInLoad);
///
/// 创建对象池
///
/// 池对象预制物
/// 池子名
/// 池是否永不销毁
/// 池对象类型
///
public static Pool CreatePool(GameObject prefab, string poolName = null, bool neverDestroyInLoad = false)
where T : Component, IPoolObject => Pool.Internal_CreatePool(prefab, poolName, neverDestroyInLoad);
}
}