2010-11-24 95 views
9

我试过在texture2d上使用dispose函数,但是这导致了问题,我很确定这不是我要使用的。如何从内容管理器中卸载内容?

我应该用什么来基本卸载内容?内容管理者是否保持自己的追踪或是否有我需要做的事情?

+0

可能重复如何XNAs Content.Load operations?](http://stackoverflow.com/questions/4242741/how-does-xnas-content-loadtexture2d-operate) – 2010-11-24 10:34:41

回答

12

看看我的答案here和可能的here

ContentManager“拥有”它加载的所有内容并负责卸载它。您应该卸载ContentManager加载的内容的唯一方法是使用ContentManager.Unload()MSDN)。

如果您对ContentManager的此默认行为不满意,可以按照this blog post中所述将其替换。

自己创建任何纹理或其他卸载-能资源,而无需通过ContentManager应该(通过调用Dispose())在Game.UnloadContent功能配置。

+0

还有一个ContentManager.Unload(bool disposing),它被描述为卸载托管内容if真正。 xna库中是否有xna内容类型需要手动处理? – Wouter 2012-04-18 10:30:43

1

如果要处理纹理,最简单的方法做到这一点:

SpriteBatch spriteBatch; 
    Texture2D texture; 
    protected override void LoadContent() 
    { 
     spriteBatch = new SpriteBatch(GraphicsDevice); 
     texture = Content.Load<Texture2D>(@"Textures\Brick00"); 
    } 
    protected override void Update(GameTime gameTime) 
    { 
     // Logic which disposes texture, it may be different. 
     if (Keyboard.GetState().IsKeyDown(Keys.D)) 
     { 
      texture.Dispose(); 
     } 

     base.Update(gameTime); 
    } 
    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 
     spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap, DepthStencilState.None, RasterizerState.CullCounterClockwise, null); 

     // Here you should check, was it disposed. 
     if (!texture.IsDisposed) 
      spriteBatch.Draw(texture, new Vector2(resolution.Width/2, resolution.Height/2), null, Color.White, 0, Vector2.Zero, 0.25f, SpriteEffects.None, 0); 

     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 

如果你想退出游戏后处置的所有内容,最好的办法做到这一点:

protected override void UnloadContent() 
    { 
     Content.Unload(); 
    } 

如果你想退出游戏后,仅设置质地:

protected override void UnloadContent() 
    { 
     texture.Dispose(); 
    } 
的[