13
\$\begingroup\$

The ContentManager in XNA 4.0 only has one Unload() method that Unloads all Assets.

I want to have some "global" Assets that are always loaded, but then I want per-Level Assets which should be Unloaded when the level is changed.

Should I create a second Instance of the ContentManager as part of the Level.cs Class? Or should I use Game.Content and then call .Dispose on the Assets I load? Or should I create my own ContentManager on top of the ReadAsset function as outlined here?

\$\endgroup\$

1 Answer 1

12
\$\begingroup\$

Create a second instance of ContentManager.

You should never Dispose() of things you load from a ContentManager (using the default implementation), as these instances are shared and reused.

Creating your own version of ContentManager is kind of orthogonal. Either way you'll be creating a second instance of ContentManager (or a class derived from it). So if the existing implementation suits your purposes - then use it, otherwise override what you want to change.

Of course, if your game is relatively small - then it won't really matter if you just use the one ContentManager and not bother unloading content between levels.

\$\endgroup\$
2
  • 5
    \$\begingroup\$ +1 and a reminder to myself to link here next time someone tries to tell me how great the singleton pattern is for asset managers. \$\endgroup\$
    – user744
    Nov 16, 2010 at 9:41
  • \$\begingroup\$ I also found the Game State Management Demo at the XNA Web Site which uses exactly this (1 global Content Manager and a 2nd one on Screens that need it). Not very intuitive, but it'll do :) \$\endgroup\$
    – Michael Stum
    Nov 16, 2010 at 16:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .