我正在使用 GameStateManagement,但我在游戏中遇到了内存问题。我将游戏的每个部分作为不同的屏幕。我有一个用于徽标的屏幕,一个用于主标题的屏幕,等等。我想我一直在错误地管理屏幕,但我一直无法找到有关我的问题的信息。
当此徽标屏幕移动到标题菜单屏幕时,它似乎并没有释放此徽标屏幕的内存资源,而且我的所有屏幕似乎都遇到了这个问题,并且内存开始堆积很多。我在做 UnloadContent 对吗?我是否需要在 UnloadContent 下对我的纹理等调用 Dispose()?我是否正确移除了屏幕?关于我做错了什么有什么想法吗?
这是我的徽标屏幕:
namespace Tower_Defense
{
class Logo : GameScreen
{
Tower_Defense curGame;
GraphicsDeviceManager graphics;
AudioManager audioManager;
Texture2D logoTexture;
int gamecycles;
public Logo(Tower_Defense game, GraphicsDeviceManager g, AudioManager am)
{
curGame = game;
audioManager = am;
graphics = g;
EnabledGestures = GestureType.Tap;
}
public override void LoadContent()
{
base.LoadContent();
logoTexture = Load<Texture2D>("Textures/Backgrounds/logo");
gamecycles = 0;
}
public override void UnloadContent()
{
base.UnloadContent();
}
public override void Draw(GameTime gameTime)
{
ScreenManager.SpriteBatch.Begin();
ScreenManager.SpriteBatch.Draw(logoTexture, Vector2.Zero, Color.White);
ScreenManager.SpriteBatch.End();
}
public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
{
gamecycles++;
if (gamecycles >= 60)
{
ScreenManager.AddScreen(new TitleScreen(curGame, graphics, audioManager, false), null);
this.UnloadContent();
ScreenManager.RemoveScreen(this);
}
base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
}
}
}