【问题标题】:How can I loop over sub folders in Assets folder?如何遍历 Assets 文件夹中的子文件夹?
【发布时间】:2020-06-03 05:40:29
【问题描述】:
string selectedPath = GetPath();
var subFolders = AssetDatabase.GetSubFolders(selectedPath);
List<string> paths = new List<string>();
foreach(string path in subFolders)
{
    paths.Add(path);
}

例如 subFolders 是 Assets/My Folder 但在我的文件夹下还有更多的子文件夹。

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

我试过了:

列表路径 = new List(); foreach(子文件夹中的字符串路径) { 路径。添加(路径); }

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

这就是我在资产中获取所选路径名的方式:

[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】:

    您可以简单地使用List&lt;T&gt;.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());
    }
    

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

    对于

    我明白了

    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)
    {
        ...
    }
    

    【讨论】:

      【解决方案2】:

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

          //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);
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-01-20
        • 2014-09-28
        • 1970-01-01
        • 2018-12-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多