how to have a scene with millions of gameObject ?

how to have a scene with millions of gameObject ?

Hello, recently i’ve been working on a map editor that use (1meter, 2 meter, 1 meter) prefabs,
my script works perfecly fine when I’m using small map ( under 1000 game object), but when I try to load huge map, for exemple 200 tile x 200 tile map, it means 40 000 game object, it takes a lot of time to start the game and it is very very laggy. I would like to have some solutions to find out how to create a game that can handle over 1 million gameObject, a 1 km x 1 km map ! Thx for helping

Hi

The simple answer is, you can’t. The GameObject is quite a ‘heavy weight’ thing in unity - it represents a ‘live’ object in the scene, and as such takes some memory and cpu to handle. A GameObject doing nothing is pretty cheap, but not cheap enough to have millions of them.

The general approach that would be taken in your situation in a typical game would be to work out how to only create the game objects that are needed. You may even just be able to create them all, but only activate those you need (I haven’t tried that approach though).

For example, on a 1km x 1km map (1000000 tiles), can the player ever see that entire map? If not, how many tiles can the player actually see at once? You will need to work this out by looking at the camera, and Instantiate/Destroy tiles that are irrelevant to the player.

If you do need to be able to see the whole map, you’ll need to start looking at lower resolution versions of it for when the player is very zoomed out. Maybe you could use a RenderTexture to render large sections of the map to a texture that can be shown on a quad instead of the hi res tiles when the player is zoomed out.

To give an example, in a 1km x 1km, there would be only be a few pixels per tile on most screens if you could see the whole thing!! So you definitely wouldn’t need to have every tile present in all its glory!

The solutions are many, and unfortunately not always particularly simple. The general rule though is that you are going to need to look for ways to limit the number of game objects required by only showing the player what they need to see.

Certainly a good starting point would be:

  • Have 1 class that manages your ‘map’, and has a list of all tiles (with as little data as possible about them - maybe just what tile they should show)
  • This class will also need to know what tiles it has actually Instantiated
  • Look at the current camera, and work out what tiles actually need to be visible
  • Destroy any you don’t need
  • Create any you do need

If you really do need to be able to see the whole map sometimes, then you’re going to have to start looking at having low-res versions of the map for when the player is very zoomed out.

-Chris

I’ve looked for tutorial on the forum but II have’nt found anything yet that take the mesh of a prefab and add it to a terrain and same for the texture of the prefab ;o is there a simple way to do it ? or should I just work on a system that load only the visible tile near the player ? :stuck_out_tongue: thanks for answering me!

In my view catlike coding has the answer. His tutorial is second to none and allows for very large maps without slow down: Hex Map 1