判断当前选中资源是否是文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/// <summary>
/// Retrieves selected folder on Project view.
/// </summary>
/// <returns></returns>
public static string GetSelectedPathOrFallback()
{
string path = "Assets";

foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
{
path = AssetDatabase.GetAssetPath(obj);
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
path = Path.GetDirectoryName(path);
break;
}
}
return path;
}

/// <summary>
/// Recursively gather all files under the given path including all its subfolders.
/// </summary>
static IEnumerable<string> GetFiles(string path)
{
Queue<string> queue = new Queue<string>();
queue.Enqueue(path);
while (queue.Count > 0)
{
path = queue.Dequeue();
try
{
foreach (string subDir in Directory.GetDirectories(path))
{
queue.Enqueue(subDir);
}
}
catch (System.Exception ex)
{
Debug.LogError(ex.Message);
}
string[] files = null;
try
{
files = Directory.GetFiles(path);
}
catch (System.Exception ex)
{
Debug.LogError(ex.Message);
}
if (files != null)
{
for (int i = 0; i < files.Length; i++)
{
yield return files[i];
}
}
}
}

// You can either filter files to get only neccessary files by its file extension using LINQ.
// It excludes .meta files from all the gathers file list.
var assetFiles = GetFiles(GetSelectedPathOrFallback()).Where(s => s.Contains(".meta") == false);

foreach (string f in assetFiles)
{
Debug.Log("Files: " + f);
}

参考链接
How to get the current selected folder of "Project" Window🔗
get folder from selection array🔗