Dương Hoàng Office cảm ơn em có j cùng a chia sẻ mọi người nhé
@xuanduongjake15443 жыл бұрын
thanks
@thainguyenquang33442 жыл бұрын
// hàm SpawnFromPool tạo thêm Object gắn vào Queue khi mà số lượng object khởi tạo ban đầu đã được bóc ra khỏi Queue để sử dụng using System.Collections; using System.Collections.Generic; using UnityEngine; public class PoolManager : MonoBehaviour { [System.Serializable] public class Pool { public string name; public GameObject prefab; public int size; } [SerializeField] public List pools; public Dictionary PoolDictionary; public static PoolManager Instance; private void Awake() { Instance = this; } // Start is called before the first frame update void Start() { PoolDictionary = new Dictionary(); foreach (Pool pool in pools) { Queue objectPool = new Queue(); for (int i = 0; i < pool.size; i++) { GameObject obj = CreateObj(pool.prefab); objectPool.Enqueue(obj); } PoolDictionary.Add(pool.name, objectPool); } } // Update is called once per frame void Update() { } public GameObject SpawnFromPool(GameObject gameObject, Vector3 position, Quaternion rotation) { if (!PoolDictionary.ContainsKey(gameObject.name)) { return null; } GameObject objectToSpawn; if (PoolDictionary[gameObject.name].Count == 0) { objectToSpawn = CreateObj(gameObject); // extend more gameobject } else { objectToSpawn = PoolDictionary[gameObject.name].Dequeue(); } objectToSpawn.transform.position = position; objectToSpawn.transform.rotation = rotation; return objectToSpawn; } public void ReturnToPool(GameObject obj) { obj.SetActive(false); PoolDictionary[obj.name].Enqueue(obj); } #region public GameObject CreateObj(GameObject gameObject) { GameObject obj = Instantiate(gameObject, this.transform); obj.name = gameObject.name; obj.SetActive(false); return obj; } #endregion }
@3DVietPro_vn Жыл бұрын
Anh sử lại như này hợp lý hơn vì hàm spawn, tự nhiên em gọi lại createobj: using System.Collections; using System.Collections.Generic; using UnityEngine; public class PoolManager : MonoBehaviour { [System.Serializable] public class Pool { public string name; public GameObject prefab; public int size; } [SerializeField] public List pools; public Dictionary PoolDictionary; public static PoolManager Instance; private void Awake() { Instance = this; } // Start is called before the first frame update void Start() { PoolDictionary = new Dictionary(); foreach (Pool pool in pools) { Queue objectPool = new Queue(); for (int i = 0; i < pool.size; i++) { GameObject obj = CreateObj(pool.prefab); objectPool.Enqueue(obj); } PoolDictionary.Add(pool.name, objectPool); } } // Update is called once per frame void Update() { } public GameObject SpawnFromPool(string nameObj, Vector3 position, Quaternion rotation, Transform parent) { if (!PoolDictionary.ContainsKey(nameObj)) { return null; } GameObject objectToSpawn; objectToSpawn = PoolDictionary[nameObj].Dequeue(); objectToSpawn.SetActive(true); objectToSpawn.transform.SetParent(parent); objectToSpawn.transform.position = position; objectToSpawn.transform.rotation = rotation; return objectToSpawn; } public void ReturnToPool(GameObject obj) { obj.SetActive(false); PoolDictionary[obj.name].Enqueue(obj); } #region public GameObject CreateObj(GameObject gameObject) { GameObject obj = Instantiate(gameObject, this.transform); obj.name = gameObject.name; obj.SetActive(false); return obj; } #endregion }