Unity获取指定资源目录下的所有文件

时间:2021-07-18 12:33:20

使用前需要引入System.IO;这个命名空间

 1 public void GetFiles()
 2 {
 3     //路径 
 4     //string path = string.Format("{0}", Application.streamingAssetsPath);
 5     string path = string.Format("{0}", @"D:\SHU170221U3D-09\Lesson14\Assets\StreamingAssets");
 6 
 7     //获取指定路径下面的所有资源文件  
 8     if (Directory.Exists(path))
 9     {
10         DirectoryInfo direction = new DirectoryInfo(path);
11         FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
12 
13         Debug.Log(files.Length);
14 
15         for (int i = 0; i < files.Length; i++)
16         {
17             if (files[i].Name.EndsWith(".meta"))
18             {
19                 continue;
20             }
21             Debug.Log("Name:" + files[i].Name);
22             Debug.Log("FullName:" + files[i].FullName);
23             Debug.Log("DirectoryName:" + files[i].DirectoryName);
24         }
25     }
26 }