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);
}
}
c# unity3d
add a comment |
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);
}
}
c# unity3d
Why do you needAddToPool
whenGetElementFromPool
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
add a comment |
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);
}
}
c# unity3d
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
c# unity3d
edited 21 hours ago
asked Nov 1 at 11:13
victor dabija
1112
1112
Why do you needAddToPool
whenGetElementFromPool
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
add a comment |
Why do you needAddToPool
whenGetElementFromPool
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Why do you need
AddToPool
whenGetElementFromPool
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