Xna 如何从内容管理器卸载内容?

Xna 如何从内容管理器卸载内容?,xna,content-management,Xna,Content Management,我尝试在texture2d上使用dispose函数,但这导致了问题,我很确定这不是我想要使用的 我应该使用什么来卸载内容?内容管理器是否自行跟踪,或者我有什么事要做?看看我的答案,可能的话 ContentManager“拥有”它加载的所有内容,并负责卸载这些内容。卸载ContentManager已加载的内容的唯一方法是使用ContentManager.unload()() 如果您对ContentManager的默认行为不满意,可以按照中所述替换它 您不需要通过ContentManager创建的任

我尝试在texture2d上使用dispose函数,但这导致了问题,我很确定这不是我想要使用的

我应该使用什么来卸载内容?内容管理器是否自行跟踪,或者我有什么事要做?

看看我的答案,可能的话

ContentManager
“拥有”它加载的所有内容,并负责卸载这些内容。卸载ContentManager已加载的内容的唯一方法是使用
ContentManager.unload()
()

如果您对ContentManager的默认行为不满意,可以按照中所述替换它


您不需要通过
ContentManager
创建的任何纹理或其他可卸载资源都应该在
游戏中进行处置(通过调用
Dispose()
)。UnloadContent
函数。

如果您想处置纹理,最简单的方法是:

    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();
    }

还有一个ContentManager.Unload(bool disposing),如果为true,则描述为也卸载托管内容。xna库中是否有管理的xna内容类型需要手动处理?@Wouter Unload在我在MSDN上看到的任何版本中都没有参数。您可能正在查看
Dispose(bool disposing)
(这是一个受保护的方法,不能在外部调用)。这是标准C#一次性模式()的一部分
Dispose(true)
在标准处理过程中被调用(使用
语句或直接调用
Dispose()
),并且自身将调用
Unload()
Dispose(false)
由终结器调用,特别是不调用
Unload
(它无法访问其他对象,它们可能已经被终结)。