Khoá học lập trình game online unity - Xử lý pool tăng tốc độ cho game - quản lý đạn

  Рет қаралды 1,331

3D Việt Pro

3D Việt Pro

Күн бұрын

Пікірлер: 7
4 жыл бұрын
Bổ ích lắm a ơi
@3DVietPro_vn
@3DVietPro_vn 4 жыл бұрын
Dương Hoàng Office cảm ơn em có j cùng a chia sẻ mọi người nhé
@xuanduongjake1544
@xuanduongjake1544 3 жыл бұрын
thanks
@thainguyenquang3344
@thainguyenquang3344 2 жыл бұрын
// 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
@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 }
HELP!!!
00:46
Natan por Aí
Рет қаралды 34 МЛН
Human vs Jet Engine
00:19
MrBeast
Рет қаралды 179 МЛН
Family Love #funny #sigma
00:16
CRAZY GREAPA
Рет қаралды 28 МЛН
Bài 10: Lập trình trao đổi dữ liệu qua giao thức Lora
13:08
Bài 50: xử lý Sprites - QuizGame  | Unity 2D
18:12
Tech Lap
Рет қаралды 23
UTS PRAKTIKUM KRIPTOGRAFI - 140810220076
16:05
Tabina adelia rafa
Рет қаралды 5
Demo ETS PBO
11:51
Thopaz Givangkara Rosadi
Рет қаралды 1
Add Model to Unity3D and Create Animation Model
9:57
FPL HN Bùi Đĩnh
Рет қаралды 71
HELP!!!
00:46
Natan por Aí
Рет қаралды 34 МЛН