迭代unity3d构建项目中的文件夹和子文件夹

时间:2017-11-07 09:34:41

标签: c# unity3d getfiles

我想从unity3d中的c#脚本填充Dictionary<string, List<AudioClip>> dict。

要填充我在unity3d中的Resources文件夹下有一个根文件夹路径。我正在尝试使用下面的代码进行迭代,并在unity3d编辑器中播放时打印所有路径。但是在我构建游戏之后,下面的工作没有按预期工作。

我如何能够遍历资源下的文件夹和子文件夹,并通过在构建unity3d游戏中传递路径来加载它们。

谢谢:)

public class IterateResoursesFolder : MonoBehaviour {


    public Text pathText;
    // Process all files in the directory passed in, recurse on any directories 
    // that are found, and process the files they contain.
    public void ProcessDirectory(string targetDirectory)
    {
        // Process the list of files found in the directory.
        string[] fileEntries = Directory.GetFiles(targetDirectory);
        foreach (string fileName in fileEntries)
            ProcessFile(fileName);

        // Recurse into subdirectories of this directory.
        string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach (string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);
    }

    // Insert logic for processing found files here.
    public void ProcessFile(string path)
    {

        var fileExtension = Path.GetExtension(path).ToLower();
        if (fileExtension == ".wav")
        {
            pathText.text = "Processed file: " + path;
            Debug.Log("Processed file: " + path);
        }
    }
    public void printPath(string path)
    {
        if (File.Exists(path))
        {
            // This path is a file
            ProcessFile(path);
        }
        else if (Directory.Exists(path))
        {
            // This path is a directory
            ProcessDirectory(path);
        }
        else
        {
            pathText.text = path + " is not a valid file or directory";
        }
    }

    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 150, 100), "print paths"))
        {
            Debug.Log("persistant data path : " + Application.persistentDataPath + "--- data path -- " +Application.dataPath );
            printPath(Application.dataPath + "/Resources");
        }
    }
}

0 个答案:

没有答案