0

I'm trying to make a custom object pooling class (this is my first time trying to avoid using Instantiate(). I know object pooling is objectively better than instantiating but I'm worried that the way I set it up might actually be worse than instantiating since I have a long List of gameobjects, and in order to find a reusable gameobject I have to use a foreach loop to loop through all the elements of the list.

using System.Collections.Generic;
using UnityEngine;

public class ObjectPooler : MonoBehaviour
{

    [SerializeField] GameObject redHurtEffect;
    [SerializeField] GameObject Arrow;
   static List<GameObject> pooledObjects = new List<GameObject>(2);
    private void Awake()
    {
        for (int i = 0; i < 15; i++)
        {
            GameObject theEffect = Instantiate(redHurtEffect);
            pooledObjects.Add(theEffect);
            theEffect.SetActive(false);
            

        }
        for (int i = 0; i < 550; i++)
        {
            GameObject theEffect = Instantiate(Arrow);
            pooledObjects.Add(theEffect);
            theEffect.SetActive(false);
        }
     
    }
    public static void Spawn(GameObject prefab, Vector3 spawnWhere, Quaternion? rotation =null, int repetitions =1, Vector2? setScale = null)
    {
        if (repetitions == 1)
        {
            Debug.Log(prefab.name);
            GameObject spawned = prefabToPooled(prefab);
            spawned.SetActive(true);
            spawned.transform.position = spawnWhere;
            if(setScale!=null || setScale.HasValue)
            {
                spawned.transform.localScale = setScale.Value;
            }
            if (rotation.HasValue)
            {
                spawned.transform.rotation = rotation.Value;
            }
      
        }
        else
        {
            for (int i = 0; i < repetitions+1; i++)
            {
           // convertedPrefab.SetActive(true);
        // convertedPrefab.transform.position = spawnWhere;

            }
        }
       
    } 
    public static GameObject Spawn(GameObject prefab, Vector3 spawnWhere, Quaternion? rotation = null, Vector2? setScale = null, Transform parent = null)
    {

          GameObject spawned =  prefabToPooled(prefab);
        Transform spawnedTransform = spawned.GetComponent<Transform>();
            Debug.Log(prefab.name);
        spawned.SetActive(true);
            spawnedTransform.position = spawnWhere;
        if (rotation.HasValue)
        {
            spawnedTransform.rotation = rotation.Value;
        }
        if (setScale != null || setScale.HasValue)
        {
            spawnedTransform.localScale = setScale.Value;
        }
        spawnedTransform.SetParent(parent);
        

        return prefabToPooled(prefab);
        
       
      
        
    }
    public static void rePool(GameObject gameObj)
    {
        gameObj.transform.SetParent(null);
        pooledObjects.Add(gameObj);
        gameObj.SetActive(false);
    }
   
    
    public static GameObject prefabToPooled(GameObject prefab)
    {
        
        string prefabName = prefab.name;
        foreach (var item in pooledObjects)
        {
            if(item.name.Contains(prefabName))
            {
                item.SetActive(true);
                pooledObjects.Remove(item);
                return item;
            }
        }
        return null;
    }
}

The performance seems the same? I tried fake instantiating arrows every frame using the object pooling, and then I tried normally instantiating, and unless I'm blind the results seemed to be the same. Also I'm worried if it'll be slower since I just started using this, I have so much more items to add to the game. The pooledObjects List might contain thousands or even tens of thousands of elements (since I want to add treasure like "diamond crown, emerald crown, coin, necklace" and I'd like to have many of those per level) Would foreach looping through those thousands of elements be worse than instantiating? Thanks!

John
  • 197
  • 1
  • 17
  • Do you insist on rolling your own solution? This is a pretty commonly used system and because of that there are many decently made yet simple Unity pooling systems on github. – Nikaas Nov 27 '21 at 06:39
  • You can also use a `Queue` instead of a `List` and push/pop accordingly; thus no iterations. – Ron Nov 28 '21 at 17:45

0 Answers0