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.

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.

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*;*

}
}
}
}

Check at the following gist:
Code snip which shows gather all files under selected folder in Unity's Project View · GitHub