- Home /
Batching objects with Instantiate for optimization
Hello, i was trying to instantiate many objects in random positions, but my frame rate didnt seem to like that, so i´ve been trying to optimize it for hours now. I discovered that batching should work, because every object is really the same one, but i can´t get it to work. Here is my code:
public GameObject candle;
public int initialSpawn;
[Min(0.0001f)]
public float velocity;
public float radius=60;
void Start()
{
GameObject[] spawnedCandle = new GameObject[initialSpawn];
for(int i=0; i< initialSpawn; i++)
{
//Sets a random point around this spawner object
gameObject.transform.Rotate(new Vector3(0, Random.Range(0f, 360f), 0));
//Creates a candle in that random point
spawnedCandle[i] = Instantiate(candle, transform.position + transform.forward * Random.Range(0f, radius), Quaternion.identity);
spawnedCandle[i].transform.Rotate(new Vector3(90, 0, 0));
}
for (int i = 0; i < initialSpawn; i++)
{spawnedCandle[i].transform.parent = transform;}
StaticBatchingUtility.Combine(spawnedCandle, candle);
}
I dont know if i should use another object as the parent in the StaticBatchingUtility.Combine() function, or im doing something wrong, but draw calls kepp getting to high numbers, instead of just a couple for each different element.
I´m also posting a photo of the result just in case it helps.
I also tried simplyfing the candle object to simply a bowl, wich is 1 mesh and of course 1 material, but still high draw calls.
Answer by rh_galaxy · Jun 03 at 04:21 AM
I haven't used StaticBatchingUtility.Combine()
and am not sure what time it saves, but doing all the Instantiate in Start()
makes it all happen in one frame... try spreading it out to be done in Update()
incrementally.
Like Instantiating 50 candles each frame and see what happens...
Answer by Cletus2000 · Jun 03 at 01:09 PM
I give up, i´ll just spawn the ones nearest to the camera and fake the further ones with particles. I´ll stay with the 3600 batches and 200fps, instead of 38 and 500fps, because its fine for this scene, but in case of wanting to take it further and put gameplay on it, it would probably be hell. I wish someone would come up with a system of prefabs acting as particles.