XNA: What is the meaning of Unload ()?

XNA games have an Unload() method, where it is assumed that the content will be uploaded. But what is the meaning of this? If all the content is unloaded, then the game must be completed, and in this case everything will collect garbage anyway, right?

+6
memory-management c # xna
source share
3 answers

As far as I understand, this is not useful for any standard applications, because, as you say, the garbage collector does things for you.

However, it’s useful to have an event called when your game goes to many things. For example, you can send a message to all clients in a multiplayer game, letting them know that you are logging out, and then you can let the garbage collector kill your network connections.

+6
source share

He is always polite to cleanse himself ... otherwise people will stop letting you play with their toys.

My best guess is that this will allow you to attach objects to the Game project and give you a way to clear them later. This will make better use of your code. Hopefully the XNA MPV or someone from the XNA team will find this and provide more information.

+2
source share

Sometimes unloading is required, especially when working with libraries that are not unloaded through the garbage collector. One example is XACT. I remember that I had to destroy the object responsible for the XACT music in the Unload event, in one specific case, when the object was not destroyed by GC ... But I forgot the real scenario ...

However, more importantly, the UnloadContent method for GameComponent classes instead of Game classes. Typically, GameComponent and DrawableGameComponent objects can use multiple resources. When an object is destroyed, you must be sure that all resources associated with the object will be destroyed, released, or sometimes put on hold.

I would advise you to leave it now. As you move forward with XNA development and for large-scale projects, you may need to use the Unload method and understand certain cases when you need to.

+1
source share

All Articles