Hello,
I recently noticed that nearly every Texture or Material I'm loading into my scenes are not getting cleared as I transition to my next scene. In the past, I circumvented these issues typically by loading to a blank or nearly empty scene while I'm loading in my next scene, but this seems to not be working in my current project.
Below is a sample of how I'm changing scenes, with some extra fluff removed for brevity. (Note: The textures/material in question are mostly NGUI atlas materials, but I have some general Sprites that are not clearing either).
public IEnumerator LoadLevel()
{
// Load into an empty scene
SceneManager.LoadScene("EmptySene", LoadSceneMode.Single);
// Asychronously load in our new scene
asyncLoad = SceneManager.LoadSceneAsync("NextScene", LoadSceneMode.Additive);
asyncLoad.allowSceneActivation = false;
// Wait until scene has finished loading
while (!asyncLoad.isDone) {
yield return new WaitForEndOfFrame
(); }
// NGUI specific call to release unused assets
UIDrawCall.ReleaseInactive();
// Unload any unused assets here and then allow our scene to load
yield return Resources.UnloadUnusedAssets();
asyncLoad.allowSceneActivation = true;
}
Typically, I would except all loading textures and materials from the previous scene to be removed since I loaded into my "EmptyScene" in Single mode, and then unloaded any unused assets before activating my next scene. However, I'm seeing a vast majority of my assets remain within memory while using the Profiler both during Editor testing and on iOS devices.
Granted, I used this method previously when all scene loading was done via the Application class, so perhaps this approach has changed and I'm not aware. I have a few assets in Resources, but they're not large and the assets that are sticking are not included or reference from there. I've also been careful not to statically reference any assets to prevent them from properly clearly.
Anything else I should be looking out for? In the Profiler, I will literally see that a single reference to the Material remains, but claims it's not being utilized by any GameObjects in the scene. If that is the case, why does calling UnloadUnusedAssets() not clear these?
Any ideas or tips would be greatly appreciated. Thanks!