如何在 "资产 "文件夹中循环使用子文件夹?

问题描述 投票:0回答:1
string selectedPath = GetPath();
var subFolders = AssetDatabase.GetSubFolders(selectedPath);
List<string> paths = new List<string>();
foreach(string path in subFolders)
{
    paths.Add(path);
}

例如子文件夹是AssetsMy Folder,但在My Folder下有更多的子文件夹,AssetDatabase.GetSubFolders不做递归,只得到第一个子文件夹,我想得到所有的子文件夹。

AssetDatabase.GetSubFolders不会递归,它只获取第一个子文件夹,我想递归获取所有子文件夹。

我试了一下。

List paths = new List();foreach(string path in subFolders){ paths.Add(path);}。

但它仍然只给我第一个子文件夹。

我是这样在Assets中获取所选路径名的。

[MenuItem("Assets/Get Path")]
private static string GetClickedDirFullPath()
{
    string clickedAssetGuid = Selection.assetGUIDs[0];
    string clickedPath = AssetDatabase.GUIDToAssetPath(clickedAssetGuid);
    string clickedPathFull = Path.Combine(Directory.GetCurrentDirectory(), clickedPath);

    FileAttributes attr = File.GetAttributes(clickedPathFull);
    return attr.HasFlag(FileAttributes.Directory) ? clickedPathFull : Path.GetDirectoryName(clickedPathFull);
}

[MenuItem("Assets/Get Path")]
private static string GetPath()
{
    string path = GetClickedDirFullPath();
    int index = path.IndexOf("Assets");
    string result = path.Substring(index);

    return result;
}
c# unity3d
1个回答
2
投票

你可以简单地使用 List<T>.AddRange 喜欢

private static string[] GetSubFoldersRecursive(string root)
{
    var paths = new List<string>();

    // If there are no further subfolders then AssetDatabase.GetSubFolders returns 
    // an empty array => foreach will not be executed
    // This is the exit point for the recursion
    foreach (var path in AssetDatabase.GetSubFolders(root))
    {
        // add this subfolder itself
        paths.Add(path);

        // If this has no further subfolders then simply no new elements are added
        paths.AddRange(GetSubFoldersRecursive(path));
    }

    return paths.ToArray();
}

因此,如

[ContextMenu("Test")]
private void Test()
{
    var sb = new StringBuilder();

    var folders = SubFolders("Assets");

    if(folders.Length > 0)
    {
        foreach (var folder in SubFolders("Assets"))
        {
            sb.Append(folder).Append('\n');
        }
    }
    else
    {
        sb.Append(" << The given path has no subfolders! >>");
    }

    Debug.Log(sb.ToString());
}

将打印出整个项目的文件夹结构。

对于

enter image description here

我得到

Assets/Example 1
Assets/Example 1/SubFolder A
Assets/Example 1/SubFolder B
Assets/Example 1/SubFolder C
Assets/Example 2
Assets/Example 2/SubFolder A
Assets/Example 2/SubFolder A/SubSubFolder A

所以在你的情况下,它将是

string selectedPath = GetPath();
var folders = SubFolders(selectedPath);
foreach(var path in folders)
{
    ...
}

1
投票

试试这段代码来获取递归的文件夹路径

    //this is your code
    string selectedPath = GetPath();
    var subFolders = AssetDatabase.GetSubFolders(selectedPath);
    List<string> paths = new List<string>();
    if(subFolders != null)
    {
        foreach(string path in subFolders)
        {
            GetAllRecursiveFolder(path,ref paths);
        }
    }
    else
    {
        paths.add(selectedPath);
    }


    public void GetAllRecursiveFolder(string currentPath,ref List<string> paths)
    {

        var subFolders = AssetDatabase.GetSubFolders(currentPath);
        if(subFolders != null)
        {
            foreach(string path in subFolders)
            {
                GetAllRecursiveFolder(path,ref paths);// Get recursive folder path, and stored in ref variable
            }
        }
        else
        {
            paths.add(currentPath);
        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.