How to run scripts in the background to avoid framerate dips

Hi im currently building a Minecraft like chunk loading system with non-Block like shapes, although i currently get 200+ fps, It freezes for a half a second when I try to load in a new chunk.

The reason for this i feel is due to my Mesh combining script that combines all meshes in a chunk before spawning it in, the script takes a second to load in non square meshes and freezes for a half a second.

Is there a way to have it running in the background and not freeze every time i load it in or maybe using multi-threads???

  • Thx Dissapoint

Yes.

Just make sure before you launch down multi-threading that you fully understand what can and cannot be done while NOT on the main thread. Far too many people assume they can do anything on any thread and nothing could be farther from the truth.

Or perhaps use ECS/DOTS. Or improve the performance of your solution.

DO NOT OPTIMIZE “JUST BECAUSE…” If you don’t have a problem, DO NOT OPTIMIZE!

If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:

Window → Analysis → Profiler

Failure to use the profiler first means you’re just guessing, making a mess of your code for no good reason.

Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.

https://discussions.unity.com/t/841163/2

Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.

Notes on optimizing UnityEngine.UI setups:

https://discussions.unity.com/t/846847/2

At a minimum you want to clearly understand what performance issues you are having:

  • running too slowly?
  • loading too slowly?
  • using too much runtime memory?
  • final bundle too large?
  • too much network traffic?
  • something else?

If you are unable to engage the profiler, then your next solution is gross guessing changes, such as “reimport all textures as 32x32 tiny textures” or “replace some complex 3D objects with cubes/capsules” to try and figure out what is bogging you down.

Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.

This sort of speculative optimization assumes you’re properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.

You probably space the calculation out over multiple frames, either with coroutines or asynchronous code.

If you are thinking of using async code, I recommend the UniTask package: GitHub - Cysharp/UniTask: Provides an efficient allocation free async/await integration for Unity.

This makes calling the Unity API during async code much more possible/stable.