Hello.
We’re witnessing an issue where lightmap textures aren’t unloaded when scenes referencing those lightmaps are unloaded.
We implemented a basic level streaming system where scenes are additively loaded and unloaded as the player progresses through the environment. These scenes are individually baked. Looking in the editor it does seem to work: the Baked Lightmaps tab of the Lighting window properly show that lightmaps are added and removed along with the scenes for which they are baked. But in builds we noticed through memory captures that textures from previously loaded lightmaps persist in memory even if the scenes they belong to are unloaded. Also it seems the lightmaps array is initially larger than in the editor and grows indefinitely when in the editor it appears to be trimmed.
There is already a call to Resources.UnloadUnusedAssets()
after scene unloads. Doing something like this
LightmapData[] lightmaps = LightmapSettings.lightmaps;
lightmaps[someIndex] = new LightmapData();
LightmapSettings.lightmaps = lightmaps;
before the Resources.UnloadUnusedAssets()
call does unload the unneeded lightmaps but messes up with the currently used ones.
Has anybody encountered the same issue? Is there something I am missing?
Some details:
- Unity 2021.3.22 (with URP)
- happens both in standalone and console builds
- bakes are done with Bakery
- the game is split in multiple scenes: 2 system scenes without any lightmap (always loaded), one “area” scene mostly containing only a directional light (set as the active scene), and the various scenes containing the environment that are loaded and unloaded as the player progresses
- environment scenes are baked independently of each other but the area scene is also loaded while baking