9

texture2d で dispose 関数を使用しようとしましたが、問題が発生しました。これは、私が意図したものではないと確信しています。

基本的にコンテンツをアンロードするには何を使用すればよいですか? コンテンツ マネージャーはそれ自体を追跡しますか、それとも私がしなければならないことがありますか?

4

2 に答える 2

12

こことおそらくここで私の答えを見てください。

ロードするすべてのContentManagerコンテンツを「所有」し、アンロードする責任があります。ContentManagerがロードしたコンテンツをアンロードする唯一の方法は、ContentManager.Unload()MSDN)を使用することです。

ContentManagerのこのデフォルトの動作に満足できない場合は、このブログ投稿で説明されているように置き換えることができます。

通過せずに自分で作成したテクスチャやその他のアンロード可能なリソースは、関数にContentManager(を呼び出してDispose())破棄する必要がありますGame.UnloadContent

于 2010-11-24T10:32:42.350 に答える
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();
    }
于 2012-04-12T18:28:18.517 に答える