Guest User

Untitled

a guest
Aug 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. XNA Texture2D caching
  2. public class ContentCache
  3. {
  4. private readonly ContentManager _content;
  5. private readonly Dictionary<string, Texture2D> _textureCache = new Dictionary<string, Texture2D>();
  6.  
  7. public ContentCache(ContentManager content)
  8. {
  9. _content = content;
  10. }
  11.  
  12. public Texture2D Load(string assetName)
  13. {
  14. Texture2D texture = null;
  15. if (!_textureCache.TryGetValue(assetName, out texture))
  16. {
  17. _textureCache[assetName] =
  18. texture = _content.Load<Texture2D>(assetName);
  19. }
  20. return texture;
  21. }
  22. }
  23.  
  24. // Preload assets
  25. static readonly string[] preloadAssets =
  26. {
  27. "Textures\texture1",
  28. };
  29.  
  30. protected override void LoadContent()
  31. {
  32. foreach ( string asset in preloadAssets )
  33. {
  34. Content.Load<object>(asset);
  35. }
  36. }
Add Comment
Please, Sign In to add comment