I’m creating a 2D, tilebased, top-down game. My map is randomly generated and should be about 1000x1000 tiles. Now my question is: is it okay to instantiate all tiles at the start of the game, or should I only instantiate those tiles that are on the screen?
I use SpriteTile for randomly generating my world. I started out with 1024x1024 and as far as I can remember it worked fine. SpriteTile doesn’t use meshes, it’s heavily optimized for this kind of games. However I populate my world with game objects like palms and stones and that pulls down performance. So I ended up dividing my world into different sectors which are game objects themselves. They contain entities and whenever you are not in a sector, the entities are disabled. The sectors are 256x256 now and that works well with AI controlled enemies, game objects etc.
1000 x 1000 is 1 million tiles. You are not going to be able to create that number of tiles (or at least I wouldn’t be able to on my reasonably high-end desktop system). If the number of tiles visible at one time is small, then you might be able to play some sort of shell game…using a set number of objects and swapping them in and changing their display to represent the 1000 x 1000.
But typically this kind of things for tiles is done by creating each tile as s quad in a mesh. Each mesh can then contain up to 16K of quads. So around 65 game object can represent all 1 million tiles. There is some starter code here if you are interested in this approach:
Search Unity Answers for ‘voxels’ or ‘minecraft’ for more information representing many game objects using meshes.
Assuming you could have 1000x1000 objects (spoiler: nope), off-screen objects do affect performance to some extent because they need to be culled every frame to determine whether they should be drawn or not.