XNA games have an Unload()
method, where content is supposed to be unloaded. But what is the point of this? If all the content is being unloaded, then the game must be exiting, in which case everything would be garbage collected anyway, right?
As far as I understand it, it's not useful for any standard uses, since as you say the garbage collector deals with things for you.
However, it's useful to have an event called when your game is exiting for many things. For example you could send a message to all clients in a multiplayer game telling them you're exiting, and then you can let the garbage collector kill your network connections.
It’s always polite to clean up after yourself… otherwise people will stop letting you play with their toys.
My best guess is that it would allow you to nest Game
objects into your project and give you a way to clean them up later. This would allow for better reuse of your code. Hopefully an XNA MPV or someone from the XNA team will find this and provide more insight.
Unload is sometimes necessary, especially when working with libraries that don't get unloaded via the Garbage Collector. One example is XACT. I remember having to destroy the object responsible for XACT music in the Unload event in one particular case where the object was not being destroyed by the GC... But I forgot the actual scenario...
More importantly however, the UnloadContent method for GameComponent classes instead of Game classes. Generally, GameComponent and DrawableGameComponent objects can make use of several resources. When the object is destroyed, you have to make sure that all resources related to the object are destroyed, releases or sometimes put in pending state.
I would suggest you leave it for now. As you move forward with XNA development, and for larger scale projects, you might need to make use of the Unload method, and understand the certain cases where you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With