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
Answer by wibble82 · Dec 16, 2015 at 10:02 AM
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
What he said :)
Also note that typically, tile-based games don't use seperate gameobjects for each tile. The terrain is created with a single mesh or texture, maybe divided into chunks. If you use Prefabs for editing the tiles, then don't instantiate a copy of a Prefab. Just insert the relevant data of the prefab, like mesh or texture data, into the existing terrain object at the right place (tile). As for objects like trees and buildings, in a game like Skyrim, only the objects near the player are actually present, while the data for the rest is kept in memory and instantiated/destroyed as he or she gets near or far.
Well :) thank you for your answer ;p in my case, the player do not have to see the whole map :P so I'll try your solution ;p and yeah my map use prefabs to instantiate the floor and there is not terrain, but ill try to add the mesh and texture data to a new terrain, cause my map is seperated in 15 linear floor :P I do not know right now how to add only the mesh and texture to a terrain but there should be a lot of tutorial on the web for it :P thanks again!
Answer by DanickCyb · Dec 16, 2015 at 04:38 PM
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 ? :P thanks for answering me!
I would focus initially on just getting it having only the visible tiles instantiated. That'll go a long way towards solving your problem and is the simplest first job to do :)
Answer by telecaster · May 27, 2017 at 03:23 PM
In my view catlike coding has the answer. His tutorial is second to none and allows for very large maps without slow down: http://catlikecoding.com/unity/tutorials/hex-map/part-1/