- Home /
Removing unused lightmaps after level unload
I'm building a large explorable area for a game I'm working on, and I've got small sub-level chunks that load in and out using LoadLevelAdditive and Destroy. But I noticed that when a chunk gets destroyed, its associated lightmaps remain in the index, and if that chunk gets re-loaded again (like if the player were to turn around and go back), it loads the lightmaps for that scene into the index a second (or third, or fourth, etc...) time.
Is there any way to run a script to remove associated lightmaps from the index during runtime, something that'd be run before the Destroy, to avoid this from happening?
Answer by Codax · Jul 08, 2011 at 08:57 PM
To get rid of all the lightmaps in the array you can use:
LightmapSettings.lightmaps = new LightmapData[0];
Resources.UnloadUnusedAssets();
If you wanted to keep a certain base set of Lightmaps, you could probably make a copy of LightmapSettings.lightmaps before you load any extra areas. Then assign that array whenever you need to.
Okay, well, this seemed to work fairly well at first. It does what you say it will do, and that's a step in the right direction. What I would really like is a script to remove only certain lightmaps, not removing the whole array. I tried to work with having to remove the whole array by "refreshing" the levels when a level is removed, but this results in a rather unsightly black void for a second or two before everything comes back into view.
Is there any possible way to remove only certain lightmaps from the array? Or am I stuck?
As he mentioned, back up your Lightmap arrays before and after and store which indexes belong to which scene you loaded.
if you unload a scene you should (I havent tested this) be able to set those lightmaps in the array to null and Resources.UnloadUnusedAssets().
Downside... Resources.UnloadUnusedAssets() kills any running coroutine. So this might not be the best option.