- Home /
How do I enumerate the contents of an asset folder?
I am writing an editor script to create a text asset containing optimised level information based on a combination of game objects in the editor and assets in an asset folder.
I can get at the game-objects in the editor easily enough but I can't find a way to enumerate my way though all the assets in the currently selected asset folder.
I can get the currently selected 'Object' easily enough using 'Object objSelected = Selection.activeObject;' but I am stuck as to how I can a) cast that to folder or get some kind of folder interface from it b) get the list of assets inside the folder c) determine the type of those assets and cast them to useful types (game-objects text assets etc...)
All help appreciated.
Answer by Jumblegreen · Apr 04, 2012 at 11:46 AM
Thanks for this. It was the lead I needed. What I wanted to do was get access to all the assets in the currently selected asset folder. This is the code I ended up with.
I hope somebody else finds this useful.
if (Selection.activeObject != null ){
Object objSelected = Selection.activeObject;
string sAssetFolderPath = AssetDatabase.GetAssetPath(objSelected);
// Construct the system path of the asset folder
string sDataPath = Application.dataPath;
string sFolderPath = sDataPath.Substring(0 ,sDataPath.Length-6)+sAssetFolderPath;
// get the system file paths of all the files in the asset folder
string[] aFilePaths = Directory.GetFiles(sFolderPath);
// enumerate through the list of files loading the assets they represent and getting their type
foreach (string sFilePath in aFilePaths) {
string sAssetPath = sFilePath.Substring(sDataPath.Length-6);
Debug.Log(sAssetPath);
Object objAsset = AssetDatabase.LoadAssetAtPath(sAssetPath,typeof(Object));
Debug.Log(objAsset.GetType().Name);
}
}
Its worth noting that the 'AssetDatabase.LoadAllAssetsAtPath ' call loads all the assets in a single FILE. It DOES NOT load all the assets in a directory which is why you have to get your own list of folder contents.
amazing!! thx for this code !!! i can get folder's obj let project can ez be fixs ! real thx!
The example above will only grab files in the immediate directory, if you want to search sub-directories too, add the SearchOption SearchOption.AllDirectories
Directory.GetFiles( folderPath,searchPattern:"*", searchOption: SearchOption.AllDirectories );
For me, Directory.GetFiles() also finds the .meta files, easily solved by adding this line right after the foreach:
if (sFilePath.EndsWith(".meta")) continue ;
8 years later and this still helped me out. BIG thanks for that "Directory.GetFiles" method. Literally was the piece I was missing to get things working as I wanted, and no one else even mentioned it in their posts
Answer by kimsama · Mar 05, 2015 at 07:27 AM
You can gather all files under the selected folder in the Project view as the following:
// You can either filter files to get only neccessary files by its file extension using LINQ.
// It excludes .meta files from all the gathers file list.
var assetFiles = GetFiles(GetSelectedPathOrFallback()).Where(s => s.Contains(".meta") == false);
foreach (string f in assetFiles)
{
Debug.Log("Files: " + f);
}
/// <summary>
/// Retrieves selected folder on Project view.
/// </summary>
/// <returns></returns>
public static string GetSelectedPathOrFallback()
{
string path = "Assets";
foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
{
path = AssetDatabase.GetAssetPath(obj);
if (!string.IsNullOrEmpty(path) && File.Exists(path))
{
path = Path.GetDirectoryName(path);
break;
}
}
return path;
}
/// <summary>
/// Recursively gather all files under the given path including all its subfolders.
/// </summary>
static IEnumerable<string> GetFiles(string path)
{
Queue<string> queue = new Queue<string>();
queue.Enqueue(path);
while (queue.Count > 0)
{
path = queue.Dequeue();
try
{
foreach (string subDir in Directory.GetDirectories(path))
{
queue.Enqueue(subDir);
}
}
catch (System.Exception ex)
{
Debug.LogError(ex.Message);
}
string[] files = null;
try
{
files = Directory.GetFiles(path);
}
catch (System.Exception ex)
{
Debug.LogError(ex.Message);
}
if (files != null)
{
for (int i = 0; i < files.Length; i++)
{
yield return files[i];
}
}
}
}
Check at the following gist: https://gist.github.com/kimsama/ff69cca140468f92d755
Your answer
Follow this Question
Related Questions
Change Default Script Folder 5 Answers
Unity Script Editor Not Working 1 Answer
Script Editors for Unity 3 Answers
The Script Equivilent of dragging a hierarchy of meshes over a preexisting prefab? 0 Answers