Managing Content

Hi there!

I was wondering if it’s possible to unload specific content at runtime.

My engine has a ContentManager (from the Game class) that loads common content items - shaders for the render pipeline, sounds, default textures etc.

When a level starts ideally I would like to load the textures & sounds specific to that level, and then unload them when the next level is queued up.

The trouble is the ContentManager only seems to support ContentManager.Unload, which will of course unload all of the common content as well.

Does anyone know of a way I can unload specific resources? If not I guess I’ll just have to make my own asset manager derived from MonoGame’s ContentManager class.

Cheers!

You can have more ContentManagers. I usually have one for permanent stuff (localization, sounds etc.) and one for non-permanent stuff.

Create a ContentManager for your level-specific textures and sounds, and a ContentManager for the common content. Then unload the level-specific ContentManager when required. You can have as many ContentManagers as is needed for your situation.

Ah OK, thanks folks.

I thought that if I created a new ContentManager instance using the IServiceProvider from my Game instance, it would just return a reference to the Game’s original content manager.

Another option is to use a custom ContentManager implementation which internally handles the individual unloading. ContentTracker can do that for you (among other things): https://contenttracker.codeplex.com/

Disclaimer: I wrote it, but it has not been updated in a long time. Check the description for a link to an Xna4 version. :slight_smile: