Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XNA: Get an array/list of resources?

Tags:

c#

xna

I am currently developing a game using XNA (School project), and I was wondering if there's a way to list all the resources during runtime because my resource files are named ###-Name## and I want to index them on the first 3-digit number.

like image 783
Not Available Avatar asked Oct 29 '10 13:10

Not Available


1 Answers

Would something like this help?

public static Dictionary<String, T> LoadContent<T>(this ContentManager contentManager, string contentFolder)
{
   //Load directory info, abort if none
   DirectoryInfo dir = new DirectoryInfo(contentManager.RootDirectory + "\\" + contentFolder);
   if (!dir.Exists)
      throw new DirectoryNotFoundException();
   //Init the resulting list
   Dictionary<String, T> result = new Dictionary<String, T>();

   //Load all files that matches the file filter
   FileInfo[] files = dir.GetFiles("*.*");
   foreach (FileInfo file in files)
   {
      string key = Path.GetFileNameWithoutExtension(file.Name);

      result[key] = contentManager.Load<T>(contentManager.RootDirectory + "/" + contentFolder + "/" + key);
   }   
   //Return the result
   return result;
}
like image 91
Neil Knight Avatar answered Oct 22 '22 06:10

Neil Knight