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(short maxCount = 0) { GameObject theRocket = null; int c = 0; foreach (var rocket in _objects) { if (!rocket.activeSelf) { theRocket = rocket; break; } c++; if (c == maxCount) return null; } if (theRocket is null) _objects.Add(theRocket = Object.Instantiate(_prefab)); return theRocket; } }