XNA Texture2D Caching

Is the class so 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>() performs internal caching. I do not want to duplicate things.

Note:

Our game is XNA 2D and is going to run on WP7 and Windows, as well as iOS and OSX, using MonoGame .

MonoGame may work differently than XNA on Windows, but I can probably look at its source to find out.

+4
source share
3 answers

Class is not needed. ContentManager does this on your behalf.

A source:

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

Note:

As for Mono ... I'm sure the implementations reflect each other well, but I can't be sure of that.

Alternatively, if you want to reload the object, you can use the optional ContentManager and then throw it away.

+4
source

It should be noted that the Mono ContentManager class now also performs caching.

Added about 2012. For future reference.

+1
source

Just cache what should be the cache in the first LoadContent method using some array of precache strings, for example:

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

Something like this is possible!

0
source

All Articles