I am building a 2D sidescroller game in Unity3D.

It's tile-based. To get better performance I divided the level into chunks. I activate these chunks when the player enters 2D Triggers and deactivate it again when he exits the 2d trigger. So I normally only have a max of 2 chunks loaded. This works good BUT I do have some loading stutters of a few frames when I load a new chunk. How can I prevent that from happening?

Thank you!

  • 1
    \$\begingroup\$ It's a difficult problem in Unity because you don't get access to threading. Unity API calls are only allowed on the main thread, which causes this sort of nonsense. \$\endgroup\$ – Almo Nov 20 '17 at 15:34
  • 2
    \$\begingroup\$ @Almo that's not entirely true. Unity has a separate loading thread for exactly this kind of case, allowing asynchronous loading. OneManOnMars, can you show us how you're currently loading your chunks? \$\endgroup\$ – DMGregory Nov 20 '17 at 16:30
  • \$\begingroup\$ But does it work? I haven't seen this work properly yet. \$\endgroup\$ – Almo Nov 20 '17 at 16:51
  • 1
    \$\begingroup\$ Are your chunks static, or dynamically tiled? I assume you mean that you have multiple Grid objects with your Tilemap layers on each Grid, and the Grids are being instantiated as they come into view, causing the stutter. Is that correct? \$\endgroup\$ – Stephan Nov 20 '17 at 17:23
  • \$\begingroup\$ The complete level is there at the beginning they are devided in several chungs and the corresponding Trigger. I deactivate what is not needed on awake. And when the player reaches the trigger of the next chunk I activate the chunk and as soon as he leaves the other chunk this one gets deactivated. \$\endgroup\$ – OneManOnMars Nov 21 '17 at 15:45

Assuming you're using the Tilemap features new in Unity 2017:

If you structure each Chunk as a Grid with multiple child Tilemap layers, you can let them all load at SceneLoad, and let the Camera frustum cull any of them that aren't in view.

This may not be ideal for target platforms with limited memory if your map is very large.

|improve this answer|||||
  • \$\begingroup\$ I am not using Unity2017 I am using 5.5 due to specific Hardware I am using. But culling via Camera frustum sounds like an ideal solution since it is much more dynamic than my system. But I am not sure how to do that. \$\endgroup\$ – OneManOnMars Nov 21 '17 at 15:47
  • \$\begingroup\$ The docs are really helpful, but frustum culling is rather hands off. Unity stops rendering any object that's fully outside its frustum by default. So just make your world segments no smaller than the size of your camera view area, and not much bigger, and that should do it. \$\endgroup\$ – Stephan Nov 21 '17 at 17:22
  • 1
    \$\begingroup\$ Thanks Stephan, will look into the documentation and see if this is helpful for my situation. \$\endgroup\$ – OneManOnMars Nov 21 '17 at 17:31
  • \$\begingroup\$ No problem, show some love with the votes, and if it works don't forget to accept! Feel free to message me if you get stuck. \$\endgroup\$ – Stephan Nov 21 '17 at 18:24
  • \$\begingroup\$ Allright I did some research about frustum culling and occlusion culling. As you said Frustum culling should work hands off. Which it does, but this also means that not rendering is my problem but other stuff seems to slow the game down. The profiler suggests that enemies that are looking for scriptofType create a spike in the profiler. So I'll try to fix that script and see if this reduces the slowdown. \$\endgroup\$ – OneManOnMars Nov 22 '17 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.