using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// This script contains the list of objects, which should be pooled. When receiving the command, it returns the object. If the object is not on the list, it creates the new object. /// [System.Serializable] public class PoolingObjects { public GameObject pooledPrefab; public int count; } public class PoolingController : MonoBehaviour { [Tooltip("Your 'pooling' objects. Add new element and add the prefab to create the object prefab")] public PoolingObjects[] poolingObjectsClass; //The list where 'pooling' objects will be stored List pooledObjectsList = new List(); public static PoolingController instance; //unique class instance for the easy access private void Awake() { if (instance == null) instance = this; } private void Start() { CreateNewList(); //Create the new list of 'pooling' objects } void CreateNewList() { for (int i = 0; i < poolingObjectsClass.Length; i++) //for each prefab create the needed amount of objects and deactivate them { for (int k = 0; k < poolingObjectsClass[i].count; k++) { GameObject newObj = Instantiate(poolingObjectsClass[i].pooledPrefab, transform); pooledObjectsList.Add(newObj); newObj.SetActive(false); } } } public GameObject GetPoolingObject(GameObject prefab) //Lookikng for the needed object by prefab name and return it { string cloneName = GetCloneName(prefab); for (int i =0; i