You can use this helper class to do this. It works by taking a given directory and using the GetFiles()
method to create a list of all the textures that are needed to be loaded. It then loads them as normal with your ContentManager
, and puts them into a 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 the textures, rather than line after line of Texture2D
s
public Dictionary<string, Texture2D> spriteContent;
...And call the method in your LoadContent
method
spriteContent = TextureContent.LoadListContent<Texture2D>(content, "textures");
Now whenever you need a texture from it, just do:
Whatever.Image = spriteContent["WhateverTexture"]
Make sure the TextureName
is the asset name of your texture.