| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- public class PoolManager : MonoBehaviour
- {
- public static PoolManager instance;
- public Dictionary<GameObject, List<GameObject>> pools;
- public Dictionary<GameObject, List<GameObject>> activeObjs;
- void Awake()
- {
- if (instance)
- {
- Destroy(gameObject);
- return;
- }
- instance = this;
- pools = new Dictionary<GameObject, List<GameObject>>();
- activeObjs = new Dictionary<GameObject, List<GameObject>>();
- }
- public static GameObject Instantiate(GameObject prefab, Vector3 pos = default, Quaternion rotation = default, Transform parent = null)
- {
- GameObject go;
- if (instance.pools.ContainsKey(prefab) && instance.pools[prefab].Count > 0)
- {
- go = instance.pools[prefab][0];
- go.transform.parent = parent;
- go.transform.position = pos;
- go.transform.rotation = rotation;
- instance.pools[prefab].RemoveAt(0);
- }
- else
- {
- go = Object.Instantiate(prefab, pos, rotation, parent);
- PoolItem poolItem = go.AddComponent<PoolItem>();
- poolItem.prefab = prefab;
- }
- go.SetActive(true);
- if (!instance.activeObjs.ContainsKey(prefab))
- {
- instance.activeObjs.Add(prefab, new List<GameObject>());
- }
- instance.activeObjs[prefab].Add(go);
- return go;
- }
- public static void Preloading(GameObject prefab,int num)
- {
- instance.activeObjs.Add(prefab, new List<GameObject>());
- for (int i = 0; i < num; i++)
- {
- GameObject go = Object.Instantiate(prefab);
- PoolItem poolItem = go.AddComponent<PoolItem>();
- poolItem.prefab = prefab;
- go.SetActive(false);
- instance.activeObjs[prefab].Add(go);
- }
- }
- public static void InstantiateAsync(string name, Vector3 pos = default, Quaternion rotation = default, Transform parent = null, UnityAction<GameObject> callback = null)
- {
- if (instance == null)
- {
- Debug.LogError("PoolManager instance not initialized!");
- callback(null);
- return;
- }
- instance.StartCoroutine(instance.ReallyLoadGameObjectAsync(name,pos,rotation,parent, callback));
- }
- private IEnumerator ReallyLoadGameObjectAsync(string name, Vector3 pos = default, Quaternion rotation = default, Transform parent = null, UnityAction<GameObject> callback = null)
- {
- ResourceRequest r = Resources.LoadAsync<GameObject>(name);
- yield return r;
- if (r.asset == null)
- {
- Debug.LogError($"Failed to load GameObject: {name}");
- callback(null);
- yield break;
- }
- GameObject prefab = r.asset as GameObject;
- if (instance.pools.ContainsKey(prefab) && instance.pools[prefab].Count > 0)
- {
- GameObject go = instance.pools[prefab][0];
- go.transform.parent = parent;
- go.transform.position = pos;
- go.transform.rotation = rotation;
- go.SetActive(true);
- instance.pools[prefab].RemoveAt(0);
- if (!instance.activeObjs.ContainsKey(prefab))
- instance.activeObjs.Add(prefab, new List<GameObject>());
- instance.activeObjs[prefab].Add(go);
- callback(go);
- }
- else
- {
- GameObject go = Object.Instantiate(prefab);
- go.transform.parent = parent;
- go.transform.position = pos;
- go.transform.rotation = rotation;
- PoolItem poolItem = go.AddComponent<PoolItem>();
- poolItem.prefab = prefab;
- if (!instance.activeObjs.ContainsKey(prefab))
- instance.activeObjs.Add(prefab, new List<GameObject>());
- instance.activeObjs[prefab].Add(go);
- callback(go);
- }
- }
- public void Recycle(PoolItem poolItem)
- {
- if (!pools.ContainsKey(poolItem.prefab))
- {
- pools.Add(poolItem.prefab, new List<GameObject>());
- }
- pools[poolItem.prefab].Add(poolItem.gameObject);
- activeObjs[poolItem.prefab].Remove(poolItem.gameObject);
- }
- public void DestroyItem(PoolItem poolItem)
- {
- if (!pools.ContainsKey(poolItem.prefab))
- {
- return;
- }
- pools[poolItem.prefab].Remove(poolItem.gameObject);
- activeObjs[poolItem.prefab].Remove(poolItem.gameObject);
- }
- }
|