Unity遍歷所選文件夾中包含某后綴名的文件路徑

//遍歷所選文件夾,查找該文件夾以及子文件夾中 后綴為 .prefab的文件路徑

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;

public class CameraMove : MonoBehaviour {
    // 在菜單來創建 選項 , 點擊該選項執行搜索代碼
    [MenuItem("Tools/遍歷項目所有文件夾")]
    static void CheckSceneSetting()
    {
        List<string> dirs = new List<string>();
        GetDirs(Application.dataPath, ref dirs);
    }
    //參數1 為要查找的總路徑, 參數2 保存路徑
    private static void GetDirs(string dirPath, ref List<string> dirs)
    {
        foreach (string path in Directory.GetFiles(dirPath))
        {
            //獲取所有文件夾中包含后綴為 .prefab 的路徑
            if (System.IO.Path.GetExtension(path) == ".prefab")
            {
                dirs.Add(path.Substring(path.IndexOf("Assets")));
                Debug.Log(path.Substring(path.IndexOf("Assets")));
            }
        }

        if (Directory.GetDirectories(dirPath).Length > 0)  //遍歷所有文件夾
        {
            foreach (string path in Directory.GetDirectories(dirPath))
            {
                GetDirs(path, ref dirs);
            }
        }
    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容