- Home /
How to get enumerate all assets of a specific class?
I would like to somehow query the asset database to find all assets of a specific class (similar to what happens in `EditorGUI.ObjectField`). I would like an array of all assets that inherit the class `SpecialScriptableObject`.
Having spent a little time searching it seems that this data is stored in a Sqlite database that is stored in the "Library" folder. Is there an API that can be used to perform such a query efficiently?
What's wrong with the AssetDatabase class? - if that doesn't help, maybe load the db file and query it LINQ style or something.
@vexe Previously I ended up using a na$$anonymous$$g convention for my assets making them easy to spot. However, I will have a much better answer for this question very soon :)
Answer by numberkruncher · May 31, 2014 at 12:20 PM
Since Unity 4.5 it is now possible to achieve this using AssetDatabase.FindAssets:
foreach (string guid in AssetDatabase.FindAssets("t:SpecialScriptableObject")) {
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
var asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(SpecialScriptableObject)) as SpecialScriptableObject;
// Do whatever you want with the asset!
}
Answer by liortal · May 09, 2014 at 08:56 AM
I have no access to the editor currently, but There are a few things you can check out:
AssetDatabase.LoadAllAssetsAtPath(path)
Documentation: https://docs.unity3d.com/Documentation/ScriptReference/AssetDatabase.LoadAllAssetsAtPath.html
This returns an Object[], you can query these objects to check whether they're of a specific type (e.g: SpecialScriptableObject)
another option may be
Object.FindObjectsOfTypeIncludingAssets(Type type)
This static method looks for all assets of a certain type, although it will also look in the scene hierarchy.
You can see how to use this function in this similar question: http://answers.unity3d.com/questions/373459/how-do-you-get-a-list-of-code-classes-from-an-edit.html
Object.FindObjectsOfTypeIncludingAssets
is obsolete, use Resources.FindObjectsOfTypeAll
ins$$anonymous$$d. It's also worth noting that they both return loaded (in memory) objects, which means if you have an inactive (not loaded) asset, it will not give you a reference to it. LoadAssetAtPath
solves this, the asset don't need to be loaded (cause as the name suggests, it loads the asset, so...) - I'm not sure given the initial question details, but I think the OP doesn't want to load anything, he just wants to query.
Answer by Loius · May 09, 2014 at 09:15 AM
I get out of memory exceptions when this gets run on a ~15gig project (and being 15 gigs is probably the problem), but I've never had issues dealing with a regular project. Should be a good starting point. For prefabs, the string[] extensions would be "prefab"; for images it might be "bmp","png", and "tga".
string[] files = AssetDatabase.GetAllAssetPaths().Where(
x=>extensions.ContainsElement(System.IO.Path.GetExtension(x).Replace(".",""))
).ToArray();
...
UnityEngine.Object thing = UnityEditor.AssetDatabase.LoadAssetAtPath(checkFile, typeof(T));
T o = (T)thing;
if ( null != o ) {
// hooray, O is an object of type T
Your answer
