Unity:如何在主文件夹中获取文件夹然后检索文件?

问题描述 投票:0回答:1

好的,可以说我们有这个层次结构:ParentFolder> ChildFolder> Files

我想检查ParentFolder是否存在,如果存在,则获取ChildFolder中的文件。我到目前为止得到的是

string MainFolderPaths = Application.persistentDataPath + "/ParentFolder";

//Check existence of folder
if (Directory.Exists(MainFolderPaths))
{
    msgText.text = "Folder exist";

    //Returns the subdirectories in MainFolderPaths
    MainFolderDirectory = Directory.GetDirectories(MainFolderPaths);

    //Go through the folders that exist in the MainFolderDirectory
    foreach(var folder in MainFolderDirectory)
    {
         //Get the files that are in each folder
         string[] files = Directory.GetFiles(folders);
         //Go through the files
         foreach(string file in files)
         {
              //read text file here
         }
    }

}
else
{
    msgText.text = "No folder found.";
}

我已经更新了代码。我想知道我在做什么吗?如果这是正确的,那么我可能正在读取错误的文件。

file unity3d directory subdirectory
1个回答
0
投票

取决于您的ParentFolder的确切位置,您应该添加一些使路径不那么相对的路径。否则,即使它可以在Unity Editor中运行,也无法在构建中运行。

例如,您可以写:

string MainFolderPaths = Application.dataPath+"/ParentFolder";

然后它将在Assets(在Unity Editor中)或_Data文件夹(在构建中)中查找此目录。更多详细信息here

关于获取子目录列表,我相信您应该使用描述为here的Directory.GetDirectories函数。

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