1
\$\begingroup\$

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!

\$\endgroup\$
6
  • 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
    Commented Nov 20, 2017 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
    Commented Nov 20, 2017 at 16:30
  • \$\begingroup\$ But does it work? I haven't seen this work properly yet. \$\endgroup\$
    – Almo
    Commented Nov 20, 2017 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
    Commented Nov 20, 2017 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\$ Commented Nov 21, 2017 at 15:45

1 Answer 1

0
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\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\$ Commented Nov 21, 2017 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
    Commented Nov 21, 2017 at 17:22
  • 1
    \$\begingroup\$ Thanks Stephan, will look into the documentation and see if this is helpful for my situation. \$\endgroup\$ Commented Nov 21, 2017 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
    Commented Nov 21, 2017 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\$ Commented Nov 22, 2017 at 15:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .