Calling this method queues up the scene unload for the following frame. In that frame, Unity destroys any objects in the scene, “de-integrates” the assets from the scene (which kicks off some unloading on the background thread), and then it de-registers/unloads the scene’s Asset Bundle.
The issue I have is with these last two steps.
The de-registration/unloading of the Asset Bundle itself requires access to the Persistent Manager, however the background thread also requires access to this manager. If the background thread is working on unloading assets when the Asset Bundle de-registration/unload is called, it will stall out the main thread as the main thread waits to get access to the Persistent Manager. You can see this by the Loading.LockPersistentManager marker in the profiler:
Note that the Asset Bundle de-registration doesn’t kick off any work on the background thread, so if there was just a small delay between the final scene unload and first de-registration event, there would be no problem.
Of course, this issue would be less severe if I only unloaded 1 or 2 scenes per frame, however it’s possible to have 20+ scenes in my use case and limiting the unloading to 1 or 2/frame would slow down the overall loading/unloading process too much.
In addition, the actual scene unload and de-registration process does not take much time, as I actually manually destroy the scene objects before unloading the scene. It is only the fight over the Persistent Manager that causes issues!
So my question:
Short of Unity re-designing this (which would be great), is there anyway to unload the scene (so that the background unloading kicks off), but delay the actual unloading/de-registration of the scene’s Asset Bundle until the next frame?
My research indicates no, that when you unload a scene it automatically unloads the Asset Bundle. I know you can call Acquire on the AsyncOperationHandle, but it’s not clear how that would work with Addressables.UnloadSceneAsync. Will the scene unload but the Asset Bundle remain? If so, how do you then unload the Asset Bundle. Is calling Release on the handle enough?