This commit is contained in:
2026-05-01 06:26:50 +08:00
parent 0055207c24
commit dff505d073
263 changed files with 13655 additions and 11640 deletions
+59 -35
View File
@@ -7,22 +7,28 @@ public class PoolData
{
public GameObject fatherObj;
public List<GameObject> poolList;
public PoolData(GameObject obj,GameObject poolParent)
public PoolData(GameObject obj,GameObject poolParent,int id)
{
fatherObj = new GameObject(obj.name);
fatherObj.transform.parent = poolParent.transform;
poolList = new List<GameObject>();
PushObj(obj);
PushObj(obj,id);
}
public void PushObj(GameObject obj,int id)
{
obj.SetActive(false);
obj = GameObject.Instantiate(obj);
obj.name = id.ToString();
poolList.Add(obj);
obj.transform.parent = fatherObj.transform;
}
public void PushObj(GameObject obj)
{
obj = GameObject.Instantiate(obj);
poolList.Add(obj);
obj.transform.parent = fatherObj.transform;
obj.SetActive(false);
}
public GameObject GetObj()
{
GameObject obj;
@@ -32,6 +38,21 @@ public class PoolData
obj.transform.parent = null;
return obj;
}
/// <summary>
/// 清理当前池子,销毁所有对象并释放父节点
/// </summary>
public void Clear()
{
foreach (var obj in poolList)
{
if (obj != null)
GameObject.Destroy(obj);
}
poolList.Clear();
if (fatherObj != null)
GameObject.Destroy(fatherObj);
}
}
public class Pool : Singleton<Pool>
@@ -50,10 +71,10 @@ public class Pool : Singleton<Pool>
else
{
GameObject temp = FishResourcesData.GetFishPrefab(id);
if (rotation == Quaternion.identity)
{
rotation = temp.transform.rotation;
}
//if (rotation == Quaternion.identity)
//{
// rotation = temp.transform.rotation;
//}
obj = Object.Instantiate(temp, position, rotation) as GameObject;//没有就读取一个,不用存入字典
obj.name = id.ToString();
}
@@ -67,11 +88,20 @@ public class Pool : Singleton<Pool>
obj = dic[id].GetObj();
obj.transform.position = position;//实例化时候的参数传过来
obj.transform.rotation = rotation;//同上
obj.transform.SetParent(parent);
}
else
{
obj = Object.Instantiate(FishResourcesData.GetFishPrefab(id), position, rotation) as GameObject;//没有就读取一个,不用存入字典
//GameObject temp = FishResourcesData.GetFishPrefab(id);
//if (rotation == Quaternion.identity)
//{
// rotation = temp.transform.rotation;
//}
obj = Object.Instantiate(FishResourcesData.GetFishPrefab(id), position, rotation,parent) as GameObject;//没有就读取一个,不用存入字典
obj.SetActive(true);
obj.name = id.ToString();
}
return obj;
}
@@ -90,39 +120,33 @@ public class Pool : Singleton<Pool>
}
else
{
dic.Add(id, new PoolData(obj, poolObj) { });
//dic.Add(id, new PoolData(obj, poolObj) { });
GameObject.Destroy(obj);
}
}
return obj;
}
public async void Return(GameObject obj, float time)//隐藏掉
//public void Clear()//切换场景时将所有缓存清除
//{
// dic.Clear();
// poolObj = null;
//}
/// <summary>
/// 清理所有池子,销毁对象并释放资源
/// </summary>
public void Clear()
{
await ReturnAsync(obj,time);
}
public async Task<GameObject> ReturnAsync(GameObject obj,float time)//隐藏掉
{
await Task.Delay((int)(time * 1000));
if (obj.activeSelf)
foreach (var kv in dic.Values)
{
if (poolObj == null)
{
poolObj = new GameObject("pool");
}
int id = int.Parse(obj.name);
if (dic.ContainsKey(id))//如果找到了这个名字
{
dic[id].PushObj(obj);
}
else
{
dic.Add(id, new PoolData(obj, poolObj) { });
}
kv.Clear();
}
return obj;
}
public void Clear()//切换场景时将所有缓存清除
{
dic.Clear();
poolObj = null;
if (poolObj != null)
{
GameObject.Destroy(poolObj);
poolObj = null;
}
}
}