using System.Collections.Generic; using UnityEngine; public class ObjectPool { public ObjectPool(GameObject prefab, int initialSize) { _prefab = prefab; _objects = new List(initialSize); for (int i = 0; i < initialSize; i++) _objects.Add(Object.Instantiate(_prefab)); } private List _objects; private GameObject _prefab; /// /// Visszaad egy új objektumot. Aktiválandó, használat után pedig deaktiválandó. /// /// Egy objektum a poolból. public GameObject GetObject(bool fixedPool = false) { GameObject theRocket = null; foreach (var rocket in _objects) { if (!rocket.activeSelf) { theRocket = rocket; break; } } if (theRocket is null) if (fixedPool) return null; else _objects.Add(theRocket = Object.Instantiate(_prefab)); return theRocket; } }