I’m getting an issue where when a scene is loaded additively via the scene manager using a CV1 we briefly drop out of our application and into the Oculus white room (i.e. the menu that’s there if you have no VR applications running).
Here are the relevant parts of the loading routine:
IEnumerator loadLevelRoutine(string name)
{
Application.backgroundLoadingPriority = ThreadPriority.Low;
// unload previous level if necessary
if (!System.String.IsNullOrEmpty(CurrentLevel))
{
Debug.Log("Unloading " + CurrentLevel);
SceneManager.UnloadScene(CurrentLevel);
yield return Resources.UnloadUnusedAssets();
}
Debug.Log("Loading " + name);
// we need have an explicit need for this as Unity kept too many assets in memory from scene to scene which resulted in out of memory exceptions
System.GC.Collect();
// wait for load
yield return SceneManager.LoadSceneAsync(name, LoadSceneMode.Additive);
// delay a bit to catch up (otherwise we get a bit of low framerate with scene visible)
yield return new WaitForSeconds(LoadDelay);
CurrentLevel = name;
}
I’m assuming this happens because “LoadSceneAsync” is just asynchronous, it’s not in parallel so can stall the main thread thus Oculus forces us to drop out for a bit.
Is there any way to avoid this without reducing the size of the loaded levels? It does this even with very small scenes that just bring in a few additional assets (slightly different routine from this which doesn’t unload or free memory).
Setting the loading priority to low I have read should reduce the chance of this happening but apparently doesn’t alleviate it altogether.
It’s probably worth noting this is using Steam/OpenVR SDK rather than the Oculus SDK.
Note that between the unload and the load we just have a solid black scene so it doesn’t matter if the framerate is briefly low but Oculus brings out of the app briefly anyway. It’s very jarring as you get a nice fade down to black but then a bright flash of white and then back to black before fading back up.
We have a persistent level that remains and the camera is in that persistent level. I’ll try it but I don’t think it will have an effect and may take us over our memory limit.
It is yielded as you can see on line 21.
Cheers
EDIT.
I was in fact not yielding the unload, so I changed Line 11 to the following:
I see the yield but it’s not yielding to let the asyncoperation continue, it’s yielding until the entire level is loaded so I don’t believe the asynchronous level load does anything as its doesn’t yield to let the async operation load asynchronously.
You would need to yield to the asycnoperation:
This is just a guess, I don’t have an Oculus, nor have I ever seen Unity explain best way to load additive asynchronous scenes. From my testing, yielding to the AsyncOperation was best results. It seemed like yielding the level load caused long delay (possibly loading entire scene in 1 frame).
However, even with yielding asyncop, it cannot break an individual file into smaller pieces, so perhaps a large asset in scene causes the FPS to take a hit. As far as I know, the ONLY way to avoid this is break files into smaller pieces because when it needs to pull something.png from storage (hd/sd) into ram for cpu/gpu to access, if that is a large file Unity cannot do anything except wait for asset to load. I had a post about this here:
Those function calls to load and unload asynchronously return an AsyncOperation so it is yielding them.
It doesn’t seem to matter if I store the async operation in a local variable first then yield it if that’s what you were getting at.
Also tried yielding a wait until that checks the isDone field.
Unfortunately there are some large files that just cannot be split up but this also seems to stall for quite small files too.
This problem is specific to Oculus so I’ll try their forums too, could be some call I can make to disable this behaviour.