2
\$\begingroup\$

So i'm using this for pooling my gameObjects in game. Every other script can ask for a pooled GameObject using GetElementFromPool and normaly i'm using return to pool when GameObject is disabled.

My doubts: 1: I'm taking the last gameObject from the List<GameObject> since the list have an array in ts implementation, so taking the last object will avoid shifts i suppose. am i right?

2: Can i avoid renaming the Instantiated GameObject in Unity? Instantiate will name it "name(Clone)"

3: Of course any other suggestion is welcome

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoolSystem : MonoBehaviour
{

    public static Dictionary<string, List<GameObject>> pool = new Dictionary<string, List<GameObject>>();

    #region cached
    private static GameObject lastReturned;
    #endregion

    public static GameObject GetElementFromPool(GameObject g)
    {
        if (!pool.ContainsKey(g.name))
        {
            lastReturned = Instantiate(g) as GameObject;
            pool.Add(g.name, new List<GameObject>());
            lastReturned.name = g.name;
            return lastReturned;
        }


        if (pool[g.name].Count == 0)
        {
            lastReturned = Instantiate(g) as GameObject;
            lastReturned.name = g.name;
        }
        else
        {
            lastReturned = pool[g.name][pool[g.name].Count - 1];
            pool[g.name].RemoveAt(pool[g.name].Count - 1);
        }

        return lastReturned;
    }

    public static void AddToPool(GameObject g)
    {

        if (!pool.ContainsKey(g.name))
        {
            pool.Add(g.name, new List<GameObject>());
        }
        pool[g.name].Add(g);

    }

}

Reviewed version:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoolSystem : MonoBehaviour
{
    public static Dictionary<string, Stack<GameObject>> pool = new Dictionary<string, Stack<GameObject>>();

    #region cached
    private static GameObject lastReturned;
    private static Stack<GameObject> lastUsedStack;
    #endregion

    public static GameObject GetElementFromPool(GameObject g)
    {
        if (pool.TryGetValue(g.name, out lastUsedStack))
        {
            if (pool[g.name].Count == 0)
            {
                lastReturned = Instantiate(g) as GameObject;
                lastReturned.name = g.name;
            }
            else
            {
                lastReturned = pool[g.name].Pop();
            }
        }
        else
        {
            lastReturned = Instantiate(g) as GameObject;
            pool.Add(g.name, new Stack<GameObject>());
            lastReturned.name = g.name;
            return lastReturned;
        }

        return lastReturned;
    }

    public static void AddToPool(GameObject g)
    {
        if (!pool.ContainsKey(g.name))
        {
            pool.Add(g.name, new Stack<GameObject>());
        }
        pool[g.name].Push(g);
    }

}
\$\endgroup\$
  • \$\begingroup\$ Why do you need AddToPool when GetElementFromPool is doing virtually exactly the same thing? \$\endgroup\$ – t3chb0t Nov 1 '18 at 12:00
  • \$\begingroup\$ AddToPool will add an existing element to pool, for example a deactivated bullet will add himself to pool. GetElementFromPool is used to recycle a previously used object, for example a weapon want to fire a bullet, with GetElementFromPool it will use a previously instantiated bullet or get a new one. \$\endgroup\$ – victor dabija Nov 1 '18 at 12:20
  • \$\begingroup\$ Why is PoolSystem inheriting from MonoBehaviour? I don't think you are using any of its functionality. \$\endgroup\$ – Tili Nov 21 '18 at 1:37
  • \$\begingroup\$ @Tili You are right there is no reason to have it. Was using monobehaviour's features just for tests. \$\endgroup\$ – victor dabija Nov 21 '18 at 9:23
  • \$\begingroup\$ AddToPool is not a great name considering what it does.. Perhaps consider using "Return" or "Release" in the function name. \$\endgroup\$ – Tili Nov 21 '18 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.