简体   繁体   English

XNA Texture2D缓存

[英]XNA Texture2D caching

Is a class such as this necessary? 这样的课是必要的吗?

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

I am curious if ContentManager.Load<Texture2D>() does it's own caching internally. 我很好奇ContentManager.Load<Texture2D>()是否在内部进行了自己的缓存。 I don't want to double-cache things. 我不想双重缓存。

Note: 注意:

Our XNA game is 2D and going to run on WP7 and Windows, and also iOS and OSX using MonoGame . 我们的XNA游戏是2D,可以在WP7和Windows上运行,也可以在iOS和OSX上使用MonoGame运行

MonoGame may function differently than XNA in Windows, but I can probably browse it's source to find that out. MonoGame的功能可能与Windows中的XNA不同,但我可能会浏览它的来源以找到它。

The class is unnecessary. 这堂课是不必要的。 ContentManager does this on your behalf. ContentManager代表您执行此操作。

Source: 资源:

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

Note: 注意:

As far as Mono goes... I'm sure the implementations mirror each other quite well, but I can't be certain on this occasion. 就Mono来说......我确信这些实现相互之间相当完美,但我不能确定这一点。

Also, if you WANT to re-load an asset, you could use an additional ContentManager and throw it away afterward. 此外,如果您想要重新加载资产,您可以使用其他ContentManager并在之后将其丢弃。

It should be noted that Mono class ContentManager now also does caching. 应该注意的是,Mono类ContentManager现在也可以进行缓存。

Added sometime in 2012. For future reference. 在2012年的某个时候添加。供将来参考。

Just cache what needs to be cache in the first LoadContent method, by using some precache string array, like: 只需使用一些预先缓存的字符串数组缓存第一个LoadContent方法中需要缓存的内容,例如:

// Preload assets
static readonly string[] preloadAssets =
{
     "Textures\\texture1",
};

protected override void LoadContent()
{
    foreach ( string asset in preloadAssets )
    {
        Content.Load<object>(asset);
    }
}

Something like that mayhaps! 这样的事可能!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM