XNA Texture2D caching

本文关键字:caching Texture2D XNA | 更新日期: 2023-09-27 18:12:09

这样的类有必要吗?

public class ContentCache
{
    private readonly ContentManager _content;
    private readonly Dictionary<string, Texture2D> _textureCache = new Dictionary<string, Texture2D>();
    public ContentCache(ContentManager content)
    {
        _content = content;
    }
    public Texture2D Load(string assetName)
    {
        Texture2D texture = null;
        if (!_textureCache.TryGetValue(assetName, out texture))
        {
            _textureCache[assetName] =
                texture = _content.Load<Texture2D>(assetName);
        }
        return texture;
    }
}

我很好奇ContentManager.Load<Texture2D>()是否在内部做自己的缓存。我不想双重缓存。

注意:

我们的XNA游戏是2D的,将在WP7和Windows上运行,也将使用MonoGame运行iOS和OSX。

MonoGame在Windows中的功能可能与XNA不同,但我可以浏览它的源代码来发现这一点。

XNA Texture2D caching

这个类是不必要的。

源:

http://forums.create.msdn.com/forums/p/31383/178975.aspx

注意:

就Mono而言…我确信这些实现可以很好地相互镜像,但在这种情况下我不能肯定。

同样,如果你想重新加载一个资源,你可以使用一个额外的ContentManager,然后把它扔掉。

应该注意的是,Mono类ContentManager现在也做缓存。

2012年某个时候添加。

只需在第一个LoadContent方法中缓存需要缓存的内容,通过使用一些预缓存字符串数组,如:

// Preload assets
static readonly string[] preloadAssets =
{
     "Textures''texture1",
};
protected override void LoadContent()
{
    foreach ( string asset in preloadAssets )
    {
        Content.Load<object>(asset);
    }
}

可能是这样的!