- Home /
Object Pooling, have some question marks
So hi everyone. I have several questions about object pooling method. I want to use object pooling for handling my bullet decal objects. As you know, simply re-using pre instantiated objects instead of instantiating and destroying is much better way because of garbage collection. In my game, i want to create bullet decals using this method. I actually did it and i have some questions about it. First of all, for your better understanding here is my code :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ObjectPool : MonoBehaviour {
public static ObjectPool currentScript;
public bool willExpand;
public GameObject concreteDecalPooled;
public int pooledConcreteDecalAmount;
List<GameObject> pooledConcreteDecals;
void Awake()
{
currentScript = this;
}
void Start()
{
pooledConcreteDecals = new List<GameObject>();
for(int i = 0; i < pooledConcreteDecalAmount; i++)
{
if(!isMultipleConcrete)
{
GameObject objConcrete = (GameObject)Instantiate(concreteDecalPooled);
objConcrete.SetActive(false);
pooledConcreteDecals.Add(objConcrete);
}
}
}
public GameObject GetPooledObject()
{
for (int i = 0; i < pooledConcreteDecals.Count; i++)
{
if(!pooledConcreteDecals[i].activeInHierarchy)
{
return pooledConcreteDecals[i];
}
}
//JUST IGNORE THIS PART FOR NOW, I HAVE QUESTION ABOUT HERE
if(willExpand)
{
GameObject obj = (GameObject)Instantiate(concreteDecalPooled);
pooledConcreteDecals.Add(obj);
return obj;
}
return null;
}
}
And in my decal script, i simply make my decals SetActive(false) after desired amount of seconds. Well I create like 50 decals at the beginning in my pool. But when i fire a weapon like machine gun, it look ridicilous because of the fact that after 50. bullet it takes the first decal and puts it in the latest hit position, so it does not look cool you know. Decals always changing positions in long period firing. To prevent this, if I increase the amount of seconds that defines when decals will be active, then no decals will be instantiated until time passes because we will be out of our 50 decaled list. Now the thing is, i put an if statement over the code, willExpand, which makes the pool to add 1 more decal each time when we are out of the list. In this way, when fire fired like 200 bullets, it looks okay, decals are realistic because we didn't re-use 150 of them, we just instantiated them. But the thing is, now i have 200 decals in my scene ? And it will continue to expand if i ever pass the limit.
So is this still an optimized way ? In maximum how many gameObjects can we keep in the scene as inactive. I am concerned because i think about using this method in different decal types, tracers, muzzles etc. Also I thought about this. If we are out of active decals in list, should I instantiate the extra decals in another prefab which has destroy script in it instead of setactive(false) ? I need someone to enlighten me, thanks in advanced :).
Your answer
Follow this Question
Related Questions
Activating GameObject(s) is slow 2 Answers
IOS Object Pooling 0 Answers
Most performance friendly way to "unrender" a GameObject? 6 Answers
Arrow fires at wrong rotation 0 Answers
Putting prefab into scene messes with the transform origin? 1 Answer