This commit is contained in:
2026-07-27 16:59:22 +08:00
parent 48863cc4f8
commit 738b2b2d36
11 changed files with 298 additions and 89 deletions
+15 -2
View File
@@ -60,6 +60,15 @@ public class Pool : Singleton<Pool>
{
public Dictionary<int, PoolData> dic = new Dictionary<int, PoolData>();//生成一个字典
private GameObject poolObj;//Scene窗口中的“pool”总收纳盒
/// <summary>获取/创建池总父节点(初始化和 Return 共用同一个 poolObj)</summary>
public GameObject EnsurePoolRoot()
{
if (poolObj == null)
poolObj = new GameObject("pool");
return poolObj;
}
public GameObject Get(int id, Vector3 position, Quaternion rotation)//返回一个预制体,
{
GameObject obj;
@@ -116,6 +125,7 @@ public class Pool : Singleton<Pool>
{
if(obj.activeSelf)
{
// ⭐ 统一用 poolObj 作为池总父节点(初始化时也用 poolObj,保证 Clear 能统一销毁)
if(poolObj==null)
{
poolObj = new GameObject("pool");
@@ -133,7 +143,7 @@ public class Pool : Singleton<Pool>
}
return obj;
}
//public void Clear()//切换场景时将所有缓存清除
//{
// dic.Clear();
@@ -144,9 +154,12 @@ public class Pool : Singleton<Pool>
/// </summary>
public void Clear()
{
// ⭐ 防重入:已经清空过直接返回
if (dic.Count == 0 && poolObj == null) return;
foreach (var kv in dic.Values)
{
kv.Clear();
if (kv != null) kv.Clear();
}
dic.Clear();