2010-10-29 63 views
6

我目前正在開發一款使用XNA(學校項目)的遊戲,並且我想知道是否有一種方法可以在運行時列出所有資源,因爲我的資源文件名爲### - 名稱##,我想將它們編入索引第一個3位數字。XNA:獲取數組/資源列表?

回答

12

會這樣的幫助嗎?

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

爲什麼我沒有想到只是掃描內容文件夾:/ – 2010-10-29 14:35:36

+0

好東西!對於任何努力使用它的人來說,我發現對於我的項目,我需要從加載調用中刪除「contentManager.RootDirectory +」,因爲我已經設置了Content.RootDirectory = Content。此方法也需要處於靜態類中才能使用。 – Mafro34 2013-10-12 01:04:12

+0

請注意,這似乎加載所有內容(因此方法的名稱)。如果你想枚舉項目,但*不加載*它們,這可能有點沉重。此外,我不認爲有什麼方法可以在不加載內容的情況下枚舉某種類型的內容。咩。我想在這種情況下離線步驟可以很好地工作。 – cod3monk3y 2014-11-13 02:22:16