You can use this helper class to do this. It works by taking the given directory and using the GetFiles() method to create a list of all the textures that need to be loaded. He then downloads them as usual with the ContentManager and puts them into the dictionary so you can use them.
public static class TextureContent { public static Dictionary<string, T> LoadListContent<T>(this ContentManager contentManager, string contentFolder) { DirectoryInfo dir = new DirectoryInfo(contentManager.RootDirectory + "/" + contentFolder); if (!dir.Exists) throw new DirectoryNotFoundException(); Dictionary<String, T> result = new Dictionary<String, T>(); FileInfo[] files = dir.GetFiles("*.*"); foreach (FileInfo file in files) { string key = Path.GetFileNameWithoutExtension(file.Name); result[key] = contentManager.Load<T>(contentFolder + "/" + key); } return result; } }
Create a dictionary to store textures, not a line after a line Texture2D s
public Dictionary<string, Texture2D> spriteContent;
... and call the method in your LoadContent method
spriteContent = TextureContent.LoadListContent<Texture2D>(content, "textures");
Now that you need texture, just do:
Whatever.Image = spriteContent["WhateverTexture"]
Make sure TextureName is the asset name of your texture.
Cyral source share