【问题标题】:Is my UnloadContent method acceptable?我的 UnloadContent 方法可以接受吗?
【发布时间】: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 方法中编写任何代码,即可制作出不会泄漏内存的高性能游戏。随着游戏结束,您的myTexturemySound 将被正确清理。
  • 手动处理不会有坏处

标签: c# xna


【解决方案1】:

由于 c# 是托管的,因此您实际上不需要卸载您的内容。这是由垃圾收集器完成的。这意味着您的内容会自动卸载。

检查this问题。

【讨论】:

  • 垃圾收集并不总是得到优化,在内存有限的情况下,可能必须卸载您的内容(即在 Xbox 360 上就是这种情况)。
  • @Francis.Beauchamp 即使游戏退出? “它使用了多个需要在游戏关闭时卸载的资产”
  • @MickyDuncan 这取决于很多事情,但我可以看到几种情况是必要的。
猜你喜欢
  • 2017-06-26
  • 2015-07-12
  • 2011-03-20
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
相关资源
最近更新 更多