XNA:如何获取资源的数组/列表?

7

我目前正在使用XNA(学校项目)开发游戏,想知道是否有一种方法可以在运行时列出所有资源,因为我的资源文件名是###-Name##,我想按照前三位数字进行索引。

1个回答

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

为什么我没想到只是扫描内容文件夹:/ - Not Available
太棒了!对于那些使用此方法遇到困难的人,我发现在我的项目中,我需要从加载调用中删除“contentManager.RootDirectory +”,因为我已经设置了Content.RootDirectory = Content。此外,这个方法需要在静态类中使用。 - Mafro34
请注意,这似乎加载了所有内容(因此方法的名称)。如果您只想枚举项目但不加载它们,则可能会有点沉重。此外,我认为没有任何方法可以枚举某种类型<T>的内容而不加载内容。嗯。我想在这种情况下,离线步骤会很好用。 - cod3monk3y

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接