Unity Generic Pool System











up vote
2
down vote

favorite












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);
}

}









share|improve this question
























  • Why do you need AddToPool when GetElementFromPool is doing virtually exactly the same thing?
    – t3chb0t
    Nov 1 at 12:00










  • 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.
    – victor dabija
    Nov 1 at 12:20










  • Why is PoolSystem inheriting from MonoBehaviour? I don't think you are using any of its functionality.
    – Tili
    yesterday










  • @Tili You are right there is no reason to have it. Was using monobehaviour's features just for tests.
    – victor dabija
    21 hours ago










  • AddToPool is not a great name considering what it does.. Perhaps consider using "Return" or "Release" in the function name.
    – Tili
    13 hours ago















up vote
2
down vote

favorite












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);
}

}









share|improve this question
























  • Why do you need AddToPool when GetElementFromPool is doing virtually exactly the same thing?
    – t3chb0t
    Nov 1 at 12:00










  • 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.
    – victor dabija
    Nov 1 at 12:20










  • Why is PoolSystem inheriting from MonoBehaviour? I don't think you are using any of its functionality.
    – Tili
    yesterday










  • @Tili You are right there is no reason to have it. Was using monobehaviour's features just for tests.
    – victor dabija
    21 hours ago










  • AddToPool is not a great name considering what it does.. Perhaps consider using "Return" or "Release" in the function name.
    – Tili
    13 hours ago













up vote
2
down vote

favorite









up vote
2
down vote

favorite











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);
}

}









share|improve this question















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);
}

}






c# unity3d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 21 hours ago

























asked Nov 1 at 11:13









victor dabija

1112




1112












  • Why do you need AddToPool when GetElementFromPool is doing virtually exactly the same thing?
    – t3chb0t
    Nov 1 at 12:00










  • 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.
    – victor dabija
    Nov 1 at 12:20










  • Why is PoolSystem inheriting from MonoBehaviour? I don't think you are using any of its functionality.
    – Tili
    yesterday










  • @Tili You are right there is no reason to have it. Was using monobehaviour's features just for tests.
    – victor dabija
    21 hours ago










  • AddToPool is not a great name considering what it does.. Perhaps consider using "Return" or "Release" in the function name.
    – Tili
    13 hours ago


















  • Why do you need AddToPool when GetElementFromPool is doing virtually exactly the same thing?
    – t3chb0t
    Nov 1 at 12:00










  • 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.
    – victor dabija
    Nov 1 at 12:20










  • Why is PoolSystem inheriting from MonoBehaviour? I don't think you are using any of its functionality.
    – Tili
    yesterday










  • @Tili You are right there is no reason to have it. Was using monobehaviour's features just for tests.
    – victor dabija
    21 hours ago










  • AddToPool is not a great name considering what it does.. Perhaps consider using "Return" or "Release" in the function name.
    – Tili
    13 hours ago
















Why do you need AddToPool when GetElementFromPool is doing virtually exactly the same thing?
– t3chb0t
Nov 1 at 12:00




Why do you need AddToPool when GetElementFromPool is doing virtually exactly the same thing?
– t3chb0t
Nov 1 at 12:00












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.
– victor dabija
Nov 1 at 12:20




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.
– victor dabija
Nov 1 at 12:20












Why is PoolSystem inheriting from MonoBehaviour? I don't think you are using any of its functionality.
– Tili
yesterday




Why is PoolSystem inheriting from MonoBehaviour? I don't think you are using any of its functionality.
– Tili
yesterday












@Tili You are right there is no reason to have it. Was using monobehaviour's features just for tests.
– victor dabija
21 hours ago




@Tili You are right there is no reason to have it. Was using monobehaviour's features just for tests.
– victor dabija
21 hours ago












AddToPool is not a great name considering what it does.. Perhaps consider using "Return" or "Release" in the function name.
– Tili
13 hours ago




AddToPool is not a great name considering what it does.. Perhaps consider using "Return" or "Release" in the function name.
– Tili
13 hours ago















active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206716%2funity-generic-pool-system%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206716%2funity-generic-pool-system%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin