- Home /
How do I get an array of names of all the sprites in a project, with an editor script?
In the editor, I'm trying to get the names of every sprite in the project. Does anyone know how I would go about doing that?
Answer by tonycoculuzzi · Jun 17, 2018 at 11:04 PM
Answering my own question a few years later, but..
AssetDatabase.FindAssets("t:Sprite")
This will return an array of string paths to all Sprite assets in the project.
Answer by Loius · Jun 17, 2014 at 02:39 AM
You'll need "using System.Linq;"
You need to find all textures, so:
string[] files = AssetDatabase.GetAllAssetPaths()
.Where(x=>extensions.ContainsElement(System.IO.Path.GetExtension(x)))
.ToArray();
"extensions" would be a string array, probably [".png",".psd",".jpg",".bmp"] or something like that. That gets you the paths to all textures in your project. You can't use Resources class functions because they only work on recently-used objects, which isn't guaranteed to find ALL textures.
For each texture, do this:
Object[] objects = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(filename));
Sprite[] sprites = objects.Select(x=>(x as Sprite)).ToArray();
That'll load all the sprites from the texture. You might need to do some type checking, I'm not sure what exactly happens if there are no sprites.
Depending on how large your project is, you'll probably want to Resources.Unload any objects you don't end up using. I'm not sure when Unity starts having memory issues with this kind of thing. :)
Once you aggregate all your sprite arrays into one giant array, you can get the names easy:
string[] names = bigSpriteArray.Select(x=>x.name).ToArray();
$$anonymous$$odified this a bit and it worked, thanks! :)