XNA:获取一个数组/资源列表?

时间:2010-10-29 13:57:20

标签: c# xna

我目前正在开发一个使用XNA(学校项目)的游戏,我想知道是否有办法在运行时列出所有资源,因为我的资源文件名为### - Name ##我想索引它们在第一个3位数字上。

1 个答案:

答案 0 :(得分:13)

这样的事情有帮助吗?

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;
}