Unity編輯器獲取資料夾下所有物件
阿新 • • 發佈:2019-01-25
有一次需要在Editor模式下獲取任意資料夾下的所有資源物件,包括子資料夾,我嘗試了AssetDatabase.LoadAllAssetsAtPath(string assetPath),理想中的Object[]沒有返回,而是給我返回了null;查閱google得知這個方法根本不是我們想要的,它是根據一個具體的object路徑,返回這個object及該object的所有子物體,所以我們就不能用這個方法獲取我們想要的資源。而我用的方法則是通過string[] arrStrAudioPath = Directory.GetFiles();這個方法獲取_Audio資料夾下所有物件的一個path,然後單個迴圈獲取所有object,下面是核心程式碼: //獲取所有object的路徑 string[] arrStrAudioPath = Directory.GetFiles(Application.dataPath + "/_Audio/", "*", SearchOption.AllDirectories); //迴圈遍歷每一個路徑,單獨載入 foreach (string strAudioPath in arrStrAudioPath) { //替換路徑中的反斜槓為正斜槓 string strTempPath = strAudioPath.Replace(@"\", "/"); //擷取我們需要的路徑 strTempPath = strTempPath.Substring(strTempPath.IndexOf("Assets")); //根據路徑載入資源 Object objAudio = AssetDatabase.LoadAssetAtPath(@strTempPath, typeof(Object)); }歡迎大家討論更好的方法,第一次寫部落格,呼呼~~~