Skip to content

Instantly share code, notes, and snippets.

@ASGrincewicz
Created October 13, 2021 00:51
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 ASGrincewicz/1d89876836002a603653b7375022a121 to your computer and use it in GitHub Desktop.
Save ASGrincewicz/1d89876836002a603653b7375022a121 to your computer and use it in GitHub Desktop.
Pool Manager for Unity Projects
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Grincewicz.PoolManager
{
///<summary>
///@author
///Aaron Grincewicz
/// @info: Holds and instantiates frequently needed destructible game objects.
///</summary>
public class PoolManager : MonoBehaviour
{
[Header("Object Pooling")]
[SerializeField] protected List<GameObject> _poolableObjects = new List<GameObject>();
[Tooltip("This list should match the Poolable Objects list indexes.")]
[SerializeField] protected List<Transform> _poolableObjectContainers = new List<Transform>();
[Tooltip("This list should correspond with Index 0 of _poolableObjects and _poolableObjectContainers.")]
[SerializeField] protected List<GameObject> _poolZero;
[Tooltip("This list should correspond with Index 1 of _poolableObjects and _poolableObjectContainers.")]
[SerializeField] protected List<GameObject> _poolOne;
[Tooltip("This list should correspond with Index 2 of _poolableObjects and _poolableObjectContainers.")]
[SerializeField] protected List<GameObject> _poolTwo;
private void Start()
{
_poolZero = GenerateObjects(_poolZero, _poolableObjects[0], _poolableObjectContainers[0], 10);
_poolOne = GenerateObjects(_poolOne, _poolableObjects[1], _poolableObjectContainers[1], 10);
_poolTwo = GenerateObjects(_poolTwo, _poolableObjects[2], _poolableObjectContainers[2],10);
}
public List<GameObject> GenerateObjects(List<GameObject> pool, GameObject prefab, Transform container, int amount)
{
pool = new List<GameObject>();
for (int i = 0; i < amount; i++)
{
if (pool.Count < amount)
{
GameObject obj = Instantiate(prefab, container);
obj.SetActive(false);
pool.Add(obj);
}
}
return pool;
}
public GameObject RequestObject(int objectIndex)
{
List<GameObject> pool = objectIndex switch
{
0 => _poolZero,
1 => _poolOne,
2 => _poolTwo,
_ => null,
};
for (int i = 0; i < pool.Count; i++)
{
if (!pool[i].activeInHierarchy)
{
pool[i].SetActive(true);
return pool[i];
}
}
GameObject newObject = Instantiate(_poolableObjects[objectIndex], _poolableObjectContainers[objectIndex]);
newObject.SetActive(true);
pool.Add(newObject);
return newObject;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment