Skip to content

Instantly share code, notes, and snippets.

@pinkas
Last active February 12, 2022 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinkas/fe2a4efe0b7d2fb81bee8adab258d6cf to your computer and use it in GitHub Desktop.
Save pinkas/fe2a4efe0b7d2fb81bee8adab258d6cf to your computer and use it in GitHub Desktop.
Very basic Unity game object pool
using System.Collections.Generic;
using UnityEngine;
public class VerySimpleObjectPool : MonoBehaviour
{
[SerializeField] private GameObject pooledObject = null;
[SerializeField] private int pooledAmount = 2;
[SerializeField] private bool canGrow = true;
[SerializeField] bool asyncInstantiate = false;
private List<GameObject> pooledObjects = null;
public static MonoBehaviour coroutineRunner;
private void Awake()
{
if (!asyncInstantiate)
{
InstantiatePooledObjects();
}
}
public void InstantiatePooledObjectsAsync(Action OnComplete)
{
if (pooledObject == null || pooledObjects != null)
{
return;
}
pooledObjects = new List<GameObject>();
coroutineRunner.StartCoroutine(InstantiatePooledObjectsCoroutine(OnComplete));
pooledObject.SetActive(false);
}
public T GetObject<T>() where T : class
{
GameObject pooledObject = GetObject();
if (pooledObject != null)
{
return pooledObject.GetComponent<T>();
}
else
{
return null;
}
}
public GameObject GetObject()
{
if (pooledObject == null)
{
return null;
}
if (pooledObjects == null)
{
InstantiatePooledObjects();
}
for (int i = 0; i < pooledObjects.Count; i++)
{
if (pooledObjects[i] == null)
{
GameObject obj = Instantiate(pooledObject);
obj.transform.SetParent(transform, false);
obj.SetActive(true);
pooledObjects[i] = obj;
return pooledObjects[i];
}
if (!pooledObjects[i].activeSelf)
{
pooledObjects[i].SetActive(true);
return pooledObjects[i];
}
}
if (canGrow)
{
GameObject obj = Instantiate(pooledObject);
pooledObjects.Add(obj);
obj.transform.SetParent(transform, false);
obj.SetActive(true);
return obj;
}
return null;
}
public List<T> GetActiveObjects<T>()
{
var activeObjects = new List<T>();
foreach (GameObject pooledObject in pooledObjects)
{
if (pooledObject.activeSelf)
{
activeObjects.Add(pooledObject.GetComponent<T>());
}
}
return activeObjects;
}
public void ReleaseObject(GameObject pooledObject)
{
pooledObject.SetActive(false);
pooledObject.transform.SetParent(transform);
}
public void ReleaseAllObjects()
{
if (pooledObjects == null)
{
return;
}
foreach (GameObject pooledObject in pooledObjects)
{
pooledObject.SetActive(false);
pooledObject.transform.SetParent(transform);
}
}
private void InstantiatePooledObjects()
{
if (pooledObject == null || pooledObjects != null)
{
return;
}
pooledObjects = new List<GameObject>();
for (int i = 0; i < pooledAmount; i++)
{
pooledObjects.Add( InstantiateObject() );
}
pooledObject.SetActive(false);
}
private IEnumerator InstantiatePooledObjectsCoroutine(Action OnComplete)
{
for (int i = 0; i < pooledAmount; i++)
{
pooledObjects.Add(InstantiateObject());
yield return new WaitForEndOfFrame();
}
OnComplete?.Invoke();
}
GameObject InstantiateObject()
{
GameObject obj = Instantiate(pooledObject);
obj.transform.SetParent(transform, false);
obj.SetActive(false);
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment