Hi everyone,
I am working on a simple side scrolling 2d game, and I am facing a little performance issue when generating too many tiles.
Right now I set up 4 tile maps, made them into prefabs, then made a generator to randomly pick from those and create a longer level (much like spelunky has various “rooms” sticked together to form a level, but procedurally chosen), each of them about 100 units wide.
The problem is that if I add even just 50 instances (which means like less than a minute run…) unity won’t even play, and just crash.
Is it better to generate each chunk on the fly, say with checkpoints, destroying what’s behind?
What if I want to create a massive level by hand instead? I might need some levels to just be set by hand rather than random.
Is there an efficient way to create a large level, also considering i would have to keep control over it?
Thanks in advance.
Use the profiler to see exactly what part of the game is lagging the most (rendering, physics, etc)
I would suggest disabling tilemaps when the player can no longer see them and then enabling them when they come into view.
If you need things to function you could just disable the sprite renderers if rendering is whats causing a lot of lag.
Thanks for your reply.
The issue is that it hangs/crashes before even starting the game. Most of the space when playing is taken up by v-sync (by a LARGE amount) but there is no in-game lag.
The issue is the pre-calculation, so I wonder if I can actually pre-generate the level, or not, as it doesn’t seem feasible right now.
Uh… have a loading screen cover the level, and then generate the levels one by one, disabling them as needed. That might work
Wow I was about to add that if I disable the maps as soon as they are generated, the load is indeed fast.
I guess my question is what’s the best practice in this case. Generate at runtime, disable if out of camera, or generate on the fly. Which one is best performance-wise?
Well, it looks like it’s the tilemap collider 2d causing the issue. If I disable that, it loads even 100 instances in a snap. The problem is that of course it needs collisions. Making it composite doesn’t change much.
Any tips on loading the other parts of the level as the game progresses?
Can you like disable the colliders when you dont need them? Or are they needed all the time?
Yes, I can disable them indeed. I am just wondering what method is more efficient performance wise, since it’s gonna be a mobile game 
It depends. Generating it all at the start of the scene will probably be the simplest but could lead to a longer loading time. I would test to see if you find the results of doing that acceptable. If you don’t, then arguably the most efficient would be to load the map as the player progresses through it. This would have its own pitfall, though, as you would need to make sure the overhead of loading the next area doesn’t cause noticeable drops in framerate.