I want to instantiate a prefab from a particular project folder with subfolders. Is it possible to do a recursive search so that it checks the parent folder and any child folders? Trying to find something in the API that will give me access to the folder structure.
I know this is over a year old question, but thought I’d answer for archive sake.
You could use Directory.GetFiles() with the SearchOption.AllDirectories and have it return a list of all files with filter. Here’s a snip I used for finding all audio files I wanted that existed in Resources:
// imports...(I hate it when people don't post imports)
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine.Audio;
// ... later in code
protected List<string> GetFiles()
{
List<string> fileList = new List<string>();
try
{
string path = Application.dataPath;
IEnumerable<string> files = Directory.GetFiles(path + "/Resources", "*.*", SearchOption.AllDirectories)
.Where(s => s.EndsWith(".mp3") || s.EndsWith(".wav") || s.EndsWith(".aif") || s.EndsWith(".ogg"));
foreach (string f in files)
{
fileList.Add(f);
}
return fileList;
}
catch (UnauthorizedAccessException UAEx)
{
Console.WriteLine(UAEx.Message);
}
catch (PathTooLongException PathEx)
{
Console.WriteLine(PathEx.Message);
}
return null;
}
Hope that helps,
John