【发布时间】:2015-06-08 23:28:36
【问题描述】:
我正在用 C# 和 XNA 4.0 制作游戏。它使用了在游戏关闭时需要卸载的多个资产。它使用多个标准变量,如 double 和 int,但也使用列表、内容管理器和纹理等。我想确保 UnloadContent 方法的当前设置能够正确卸载内容。代码设置类似如下。
//These are the above-standard variables (Not like int, double and float)
List<string> myStrings; //List of strings
List<int> myInts; //List of integers
ContentManager contentOne; //This loads specific content
ContentManager contentTwo; //This loads other specific content
Texture2D myTexture; //This is loaded with "contentOne"
SoundEffect mySound; //This is loaded with "contentTwo"
//This is where the content is unloaded
void UnloadContent()
{
//All of the content managers are unloaded
contentOne.Unload();
contentTwo.Unload();
//All of the lists are cleared
myInts.Clear();
myStrings.Clear();
//This is an added precaution
Dispose()
}
如您所见,该方法会清除列表并卸载内容管理器。但是,它不会对各个变量做任何事情(方法中没有修改纹理和音效)。我需要做的就是卸载内容管理器并清除列表吗?另外,“Dispose()”是不是必须调用的方法?
【问题讨论】:
-
“托管代码”例如 C# 为您管理内存,它跟踪堆上的对象,当不再需要或使用它们时,它会释放它们占用的内存而无需任何输入您,您无需在
UnloadContent方法中编写任何代码,即可制作出不会泄漏内存的高性能游戏。随着游戏结束,您的myTexture和mySound将被正确清理。 -
手动处理不会有坏处