How do I access the list of all assets in a project?

Suppose I have a project containing a number of assets. Is it possible to iterate through the whole set of assets that are shown in the Project panel?

In short, I'd like to write code similar to

foreach ( Object o in AssetDatabase.AllAssets )
  if ( o is GameObject )    // Prefab

but there is no `AssetDatabase.AllAssets` to be found anywhere, or is there?

It is possible to just use `Directory.GetFiles()` and then load them individually with `AssetDatabase.LoadAssetAtPath()`, but there are some drawbacks to this approach:

  1. Speed, obviously. Supposing I need to perform some actions on 5% of the assets...
  2. When using Unity with SVN, extra files are created, and need to be filtered out. True, this can be done by simply filtering out "*.meta", but still.
  3. The whole idea of doing this by hand when the assets are sitting there in the Project panel, right before my eyes, with types and icons, seems unnatural.

The project panel uses internal APIs for it's work, so, you'd probably have to use Directory.GetFiles(). It's not that bad really, I can see your points 2. and 3. though both don't seem to be big issues. Why would you think speed would be a problem here?

Actually an AssetDatabase.AllAssets property as you suggest would also not really be feasible, because it would require all objects to be loaded, which can mean Gigabytes of memory on big projects.

foreach(string s in AssetDatabase.GetAllAssetPaths()
.Where(s => s.EndsWith(“.psd”, StringComparison.OrdinalIgnoreCase)
|| s.EndsWith(“.tiff”, StringComparison.OrdinalIgnoreCase)))
Texture2D texture = (Texture2D) AssetDatabase.LoadAssetAtPath(s, typeof(Texture2D));

I'm not entirely sure about editor scripting, but Unity limits what files you can access programmaticly intentionally. The scene is the parent for the resource manager.

The only way that I know of to get access to assets via script is to use the Resources class. Unfortunately, that requires your files to be under a directory named /Resources/, as well as implies that all of those assets are loaded all the time.

I wrote an Editor extension here: which contains code to do what you suggest, and it works exactly the way you mention. Also, I am glad you mentioned .meta files; I think I forgot to exclude those in the version I have on the wiki. I have an update to post soon that does.

@jonas it’s true what you say about memory, but doing an UnloadUnusedAssets immediately afterward seems to make it survivable. :slight_smile: In some cases you can also use the strategy of returning all the pathnames first and filtering on those before loading actual assets.