【问题标题】:Unity: How to get folder in a main folder then retrieve the files?Unity:如何在主文件夹中获取文件夹然后检索文件?
【发布时间】:2023-03-29 15:12:01
【问题描述】:

好吧,假设我们有这个层次结构: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.";
}

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

【问题讨论】:

  • 好吧,你从来没有告诉过在哪里找到 ParentFolder。您的 MainFolderPaths 变量包含 relative 路径,而您从不说从哪里开始查找。
  • 我实际上是在使用`Application.persistentDataPath`
  • 你现在是我发表评论时你还不是。

标签: file unity3d directory subdirectory


【解决方案1】:

根据您的 ParentFolder 的确切位置,您应该添加一些使路径不那么相关的内容。否则,即使它可以在 Unity Editor 中运行,它也不会在 build 中运行。

你可以例如写:

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

然后它将在 Assets(在 Unity 编辑器中)或 _Data 文件夹(在构建中)中查找此目录。更多详情here.

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

【讨论】:

  • 是的,我实际上在使用 Application.persistentDataPath。但是,如果获取文件的方法是正确的,那么它就是我搞砸的读取方法。
猜你喜欢
  • 1970-01-01
  • 2014-03-20
  • 2015-06-04
  • 1970-01-01
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 1970-01-01
相关资源
最近更新 更多