I’m searching for something like:
Object[] objs = AssetDatabase.Find (typeof (ScriptableObject));
Does this exists? If not, how does ProjectWindow (or a SearchableEditorWindow) get those? How does the search manage to filter by type? (t:Material
)
Unity has made Object. FindObjectsOfTypeIncludingAssets
obsolete and recommends you use Resources. FindObjectsOfTypeAll
but this will not find all assets.
I have written a small piece of code that you can use in Editor only that takes a generic type and will return all objects in your project regardless of if they have been loaded yet.
public static List<T> FindAssetsByType<T>() where T : UnityEngine.Object
{
List<T> assets = new List<T>();
string[] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(T)));
for( int i = 0; i < guids.Length; i++ )
{
string assetPath = AssetDatabase.GUIDToAssetPath( guids *);*
Here is a c# one for getting all assets in project of type with a certain file extension
/// <summary>
/// Used to get assets of a certain type and file extension from entire project
/// </summary>
/// <param name="type">The type to retrieve. eg typeof(GameObject).</param>
/// <param name="fileExtension">The file extention the type uses eg ".prefab".</param>
/// <returns>An Object array of assets.</returns>
public static Object[] GetAssetsOfType(System.Type type, string fileExtension)
{
List<Object> tempObjects = new List<Object>();
DirectoryInfo directory = new DirectoryInfo(Application.dataPath);
FileInfo[] goFileInfo = directory.GetFiles("*" + fileExtension, SearchOption.AllDirectories);
int i = 0; int goFileInfoLength = goFileInfo.Length;
FileInfo tempGoFileInfo; string tempFilePath;
Object tempGO;
for (; i < goFileInfoLength; i++)
{
tempGoFileInfo = goFileInfo*;*
if (tempGoFileInfo == null)
continue;
tempFilePath = tempGoFileInfo.FullName;
tempFilePath = tempFilePath.Replace(@"", “/”).Replace(Application.dataPath, “Assets”);
Debug.Log(tempFilePath + "
" + Application.dataPath);
tempGO = AssetDatabase.LoadAssetAtPath(tempFilePath, typeof(Object)) as Object;
if (tempGO == null)
{
Debug.LogWarning(“Skipping Null”);
continue;
}
else if (tempGO.GetType() != type)
{
Debug.LogWarning("Skipping " + tempGO.GetType().ToString());
continue;
}
tempObjects.Add(tempGO);
}
return tempObjects.ToArray();
}
AssetDatabase.Find(“t: ScriptableObject”)
You can test the filters in your searchbar before
Documentation: Unity - Scripting API: AssetDatabase.FindAssets
glitchers answer is correct but here’s an iterator version of it:
public static IEnumerable<T> FindAssetsByType<T>() where T : Object {
var guids = AssetDatabase.FindAssets($"t:{typeof(T)}");
foreach (var t in guids) {
var assetPath = AssetDatabase.GUIDToAssetPath(t);
var asset = AssetDatabase.LoadAssetAtPath<T>(assetPath);
if (asset != null) {
yield return asset;
}
}
}