I want to populate Dictionary<string, List<AudioClip>>
a dict from c# script in unity3d.
To Populate that I have a root folder path under Resources
folder in unity3d.I am trying to use below code to iterate and its prints all paths while playing in unity3d editor. But after I build the game the below is not working as expected.
How do I will be able to iterate through folders and subfolders under Resources and load them by passing path in build unity3d game.
Thanks :)
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");
}
}
}