1

I'm making a game in C# and XNA 4.0 and I want to use a texture to show players a screenshot of each level in the level select menu. enter image description here Since I'm only using one texture, I need to load a new one every time that the player highlights a new option on the menu. I do so with code similar to the following:

Texture2D levelTexture;

if(user.HighlightsNewOption)
{
    //Notice that there is no form of unloading in this if statement
    string file = levelSelectMenu.OptionNumber.ToString();

    levelTexture = Content.Load<Texture2D>(file);
}

However, I fear that this current setup will cause memory usage issues. Would this problem be solved by unloading the texture first and then loading the new one? If so, how can I do this?

1 Answers1

0

You can't unload a single texture. However you can unload an entire ContentManager instance's contents all at once using ContentManager.Unload().

The way I approached memory management in XNA is to modularize the game screens and menus into components. Each gets their own ContentManager instances, unloading them when no longer needed.

Using that philosophy, I wouldn't worry about the memory usage here. Give your menu its own ContentManager instance. Load all of the screenshots at once when loading the content for your menu. When the menu is exited, unload it and unload its content with ContentManager.Unload().

Your player's device can probably handle a few screenshots. If you've got something like 100 levels you could limit each to a small thumbnail.

If you really, really need to, you can make each screenshot its own component which has its own ContentManager, but that is not recommended.

For further reading try: How do I unload content from the content manager?

Community
  • 1
  • 1
  • Right. I was originally using a list of Texture2D variables. There are 64 levels, so I would have a list of 64 textures loaded at once. So, you're saying that I should load the list of level textures using a new ContentManager and unload it whenever the player isn't using the level select menu (and load it again when they are)? –  May 28 '15 at 16:08
  • Also, the game is intended for Windows PCs rather than something like Android, in case that's of any interest. –  May 28 '15 at 16:10
  • That sounds about right. I'd give it a try and monitor memory usage. If it's too high you could try changing the texture format or lowering the resolution. (Edit: Also, if it takes a noticeable amount of time to load the textures, you could load them one by one when the user clicks on the level description the same way you wrote it in the question. Then, when you're done, unload the dedicated ContentManager) – Dark Flame Master May 28 '15 at 17:06