- Pool에서 List와 Dictionary 차이
1. List : 하나의 풀로 관리 -> 두개의 오브젝트에서 각각을 관리(bulletPool, monsterPool)
- 단일 타입을 관리한다.using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEngine.Pool; public class ObjectPool : MonoBehaviour { public GameObject prefab; List<GameObject> pool = new List<GameObject>(); public int poolSize; public string key; void Start() { InitializePool(poolSize, key); } public GameObject Get(string objectType) { if (pools.ContainsKey(objectType)) { foreach (GameObject obj in pools[objectType]) { if (!obj.activeSelf) { obj.SetActive(true); return obj; } } } return null; } public void Release(GameObject obj) { obj.SetActive(false); obj.transform.SetParent(null); } private void InitializePool(int size, string objectType) { if (!pools.ContainsKey(objectType)) { pools[objectType] = new List<GameObject>(); } for (int i = 0; i < size; i++) { GameObject obj = Instantiate(prefab); obj.SetActive(false); obj.name = objectType + "_" + i; pools[objectType].Add(obj); } } }2. Dictionary : 두개의 풀로 나누어 관리 -> 하나의 오브젝트에서 관리(PoolManaer)
- 여러 타입을 관리한다.using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEngine.Pool; public class ObjectPool : MonoBehaviour { private Dictionary<string, List<GameObject>> pools = new Dictionary<string, List<GameObject>>(); public GameObject bulletPrefab; public GameObject monsterPrefab; public int bulletPoolSize; public int monsterPoolSize; string bulletKey = "Bullet"; string monsterKey = "Monster"; void Start() { InitializePool(bulletPoolSize, bulletKey, bulletPrefab); InitializePool(monsterPoolSize, monsterKey, monsterPrefab); } public GameObject Get(string objectType) { if (pools.ContainsKey(objectType)) { foreach (GameObject obj in pools[objectType]) { if (!obj.activeSelf) { obj.SetActive(true); return obj; } } } return null; } public void Release(GameObject obj) { obj.SetActive(false); obj.transform.SetParent(null); } private void InitializePool(int size, string objectType, GameObject prefab) { if (!pools.ContainsKey(objectType)) { pools[objectType] = new List<GameObject>(); } for (int i = 0; i < size; i++) { GameObject obj = Instantiate(prefab); obj.SetActive(false); obj.name = objectType + "_" + i; pools[objectType].Add(obj); } } }