Rendering Spike When Switching Levels

So I have two levels divided by a door. I’m keeping the second level hidden while the player plays through the first area, then as the door opens I’m turning on the second level just before the player enters it.

As soon as I switch the second level on, I get a huge spike in rendering time for one frame, and then it calms down and runs normally. I’m trying to find a way to get rid of this big hitch.

I’ve tried a number of ways of hiding the levels:

  • Deactivating game objects causes an even worse spike as calling OnEnable on numerous objects tends to be costly

  • Turning off all of the renderers in the scene actually works better than deactivating the root object, I still have this spike in render time though

  • Set each level to have a unique layer, then adjust the culling mask of my main camera. I thought this might help because I’m not activating/deactivating any components, but nope.

  • Unfortunately using “portals” and occlusion culling won’t work for me since I’m building these levels proceduraly.

Any ideas what is causing this super long frame and how to fix it? The bigger the scene I am transitioning to the worse the spike. Even on PC for a large scene I’ll have one frame that takes 600 ms to render the scene, then it drops down to ~5 ms next frame.

Thanks for your help!

I seem to always end up answering my own questions…

Anyway maybe this will help someone else. I guess the first time Unity renders something it has to load some infomation (textures, etc…) onto the GPU, so the first time you reveal something huge like a big level to the camera there is a big spike.

To fix the spike I load all my levels, make them all visible but put up a load screen over top. Then I fly my game camera around randomly so hopefully it sees most of the stuff it has to draw later. It’s a pretty dirty solution, but it seems to work fine.