Tuesday, May 16, 2017

xna 4.0 - Disposing only certain resources in XNA?


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?



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...