Create huge amount of GameObjects without FPS drop? (about 23k objects)

Good day community!

I am working on a project which empiles a database. For now I am loading the data from a .CSV file which contains about 23k rows, each row equals 1 item.

The prefab I use for this purpose is a cube with a script attached to it with some string variables like id, name, description and quantity, this to set and get properties of the objects, so I can manipulate it.

What I need to do is, by clicking a button I need to Instantiate the prefab, then assig all the corresponding strings from the .csv to the prefab string variables, and finally add all the GameObjects to a List.

In a few words I need to create a cube for each object in the database and then, position it and render it on screen.

I already did that, but the fact that there are about 23k objects, Unity freezes, so I was tinking about a separate thread and found this video:

But I can not use “someGameObject.GetComponent ().SomeMethod();” from a thread which is not the main thread.

So I wonder if there is a better way to Instantiate prefabs, setting propoerties and then adding toa list?

Thank you

I would avoid making them live objects and instead manage them from a single script that holds the data of each one, instead of needing to GetComponent on instantiated objects. You could then also use direct GPU instancing to pump up the efficiency drastically as well.

Depending on your needs, perhaps you can do it over several frames?

IEnumerator GenerateObjects ()
{
    int objectsPerFrame = 100;
    List <GameObject> gameObjects = new List<GameObject> ();
    while (gameObjects.Count < 23000)
    {
        for (int i = 0; i < objectsPerFrame; i++)
        {
            //Instantiate stuff
        }
        yield return null;
    }
}
2 Likes

Thank you for your response, I think I will create a list of a group of objects, then add that list to an other list, so I won’t have to loop trough every 23000 objects per frame, instead I will only loop trough thosee from a specific list.

But I still want to hear more ideas, thanks

My idea wasn’t to make any actual objects. Just instantiate a list of the CLASSES that hold values, and render your cubes using GPU instancing, don’t instantiate new cubes.

https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstancedIndirect.html

https://forum.unity3d.com/threads/drawmeshinstanced-option-to-provide-per-instance-material-property-block.435716/

https://www.youtube.com/watch?v=p6F7M2BSTA8

https://www.youtube.com/watch?v=E8d4xDG7bc4

https://www.reddit.com/r/Unity3D/comments/5idqs9/400000_cubes_compute_shader_fluid_simulation/

1 Like

Cool man! I will chak it righ away! Thank you! :smile: