Advertisement
JimTheJam

Unity GameObject Pooler

Apr 21st, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.29 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Sirenix.OdinInspector;
  6. using UnityEngine;
  7.  
  8. public class Pooler : MonoBehaviour {
  9.     public static Pooler Instance;
  10.    
  11.     [ListDrawerSettings(ShowIndexLabels = true, ListElementLabelName = "pooledObject")] [SerializeField] [ReadOnly]
  12.     private List<PooledObject> pooledObjects;
  13.  
  14.     private void Awake() {
  15.         if (Instance == null) {
  16.             Instance = this;
  17.         }
  18.         else {
  19.             Debug.LogError("Found duplicate Pooler");
  20.             Destroy(this);
  21.         }
  22.  
  23.         foreach (var pObj in pooledObjects)
  24.             StartCoroutine(SpawnObjects(pObj, true));
  25.     }
  26.  
  27.     public static GameObject SpawnObject(GameObject targetObject, Vector3 spawnLocation) {
  28.         return Instance.DoSpawnObject(targetObject, spawnLocation);
  29.     }
  30.    
  31.     private GameObject DoSpawnObject(GameObject targetObject, Vector3 spawnLocation) {
  32.        
  33.         if (targetObject == null) {
  34.             Debug.Log($"NOTE: Tried to spawn a non-existent object! Oops!");
  35.             return null;
  36.         }
  37.        
  38.         var pool = pooledObjects.FirstOrDefault(o => o.pooledObject.name == targetObject.name);
  39.         var obj = pool?.spawnedObjects.FirstOrDefault(o => o.activeSelf == false);
  40.        
  41.         if (pool == null) {
  42.            
  43.             var newPool = new PooledObject {
  44.                 poolContent = Instantiate(new GameObject(), this.transform).transform,
  45.                 pooledObject = targetObject,
  46.                 initialAmount = 3,
  47.                 increaseBoundsAmount = 3
  48.             };
  49.             newPool.poolContent.name = targetObject.name;
  50.             Debug.Log($"NOTE: Had to create a new pool for {targetObject.name}");
  51.             pooledObjects.Add(newPool);
  52.             StartCoroutine(SpawnObjects(newPool));
  53.             return SpawnObject(targetObject, spawnLocation);
  54.         }
  55.  
  56.         if (obj == null) {
  57.             if (pool.increaseBoundsAmount <= 0) return null;
  58.             StartCoroutine(SpawnObjects(pool));
  59.             return SpawnObject(targetObject, spawnLocation);
  60.         }
  61.        
  62.         obj.transform.position = spawnLocation;
  63.         obj.SetActive(true);
  64.         return obj;
  65.     }
  66.  
  67.     private IEnumerator SpawnObjects(PooledObject poolObj, bool init = false) {
  68.         var amountToSpawn = init ? poolObj.initialAmount : poolObj.increaseBoundsAmount;
  69.         while (amountToSpawn > 0) {
  70.             var obj = Instantiate(poolObj.GetPooledObject(), poolObj.poolContent);
  71.             obj.name = poolObj.GetPooledObject().name;
  72.             poolObj.spawnedObjects.Add(obj);
  73.             obj.SetActive(false);
  74.             amountToSpawn--;
  75.         }
  76.  
  77.         yield return new WaitForEndOfFrame();
  78.     }
  79. #if UNITY_EDITOR
  80.     [Button]
  81.     private void SortPooledObjects() {
  82.         pooledObjects = pooledObjects.OrderBy(o => o.pooledObject.name).ToList();
  83.     }
  84. #endif
  85. }
  86.  
  87. [Serializable]
  88. public class PooledObject {
  89.     public Transform poolContent;
  90.     public GameObject pooledObject;
  91.     public int initialAmount;
  92.     public int increaseBoundsAmount;
  93.     public List<GameObject> spawnedObjects = new();
  94.  
  95.     public GameObject GetPooledObject() {
  96.         pooledObject.SetActive(false);
  97.         return pooledObject;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement