using System.
Collections;
using System.Collections.Generic;
using UnityEngine;
//this class is semi-generic, because it spawns a prefab, which is ALWAYS a
gameobject
public abstract class GameObjectPool<T> : Singleton<GameObjectPool<T>>
where T : Component
{
[SerializeField] protected GameObject prefab;
private Queue<GameObject> objects = new Queue<GameObject>();
new private void Awake()
{
base.Awake();
}
//returns a GameObject from pool or creates one if none is left
public GameObject GetObject(Vector3 spawnPos)
{
GameObject obj;
if (objects.Count == 0)
{
obj = (GameObject)Instantiate(prefab, spawnPos, Quaternion.identity);
}
else
{
obj = objects.Dequeue();
obj.transform.position = spawnPos;
}
obj.gameObject.SetActive(true);
return obj;
}
//puts a gameObject back to the pool
public void ReturnToPool(GameObject obj)
{
obj.gameObject.SetActive(false);
objects.Enqueue(obj);
}
}