- Home /
How can I prevent frame spikes when activating parts of a level?
My project has large linear scenes the player travels through. For performance, we group the scenes in to chunks and keep them "inactive" and set them active when the player gets close to them. My problem is that calling the call to SetActive can cause a large (> 33ms) frame spike. Is there a better way to go about this?
It would be fine if the time spent was at before the level starts, but it seems to have no effect to leave every section active for a few seconds before hiding it.
Answer by KriskyD · Jan 31, 2018 at 10:19 PM
It may be caused by loading new objects or shaders for the first time. An easy solution is to leave all your chunks active in the scene and set them inactive after game starts. Another way around is to read about Unity methods like Shader.WarmupAllShaders() and similar and to try them out.
Yes, I think this is part of the problem but there are other issues as well. I still see the spikes even when I do leave them active for a few seconds. I also am using Shader.WarmupAllShaders at level start as well. Even still GameObject.Activate still takes a long time...
@rt_jscott Try using Coroutines. You can save every gameObject in chunk to list or array and then activate few of them in every frame with one function.
Oh that's a very good idea! I'll try that out. Thanks!
Answer by Friedemann_A · Feb 01, 2018 at 02:48 PM
I don't know if this works for you as it would probably require lots of rework in your scene-structure but I think many people split their bigger levels up into multiple scene and use LoadSceneAsync for stuff like this: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html
If you've played "Inside" I think that's what they used there for example.
Hope this helps!
In fact we've already split our level up in to multiple scenes just so that we can have multiple people work on them. We've got enough memory to load them all, so I hadn't considered bothering to LoadSceneAsync them. Thanks!