using System.Collections.Generic;
using UnityEngine;
namespace UnityEditor.Rendering
{
/// Set of helpers for AssetDatabase operations.
public static class AssetDatabaseHelper
{
///
/// Finds all assets of type T in the project.
///
/// Asset type extension i.e ".mat" for materials. Specifying extension make this faster.
/// The type of asset you are looking for
/// An IEnumerable off all assets found.
public static IEnumerable FindAssets(string extension = null)
where T : Object
{
string query = BuildQueryToFindAssets(extension);
foreach (var guid in AssetDatabase.FindAssets(query))
{
var asset = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(guid));
if (asset is T castAsset)
yield return castAsset;
}
}
///
/// Finds all assets paths of type T in the project.
///
/// Asset type extension i.e ".mat" for materials. Specifying extension make this faster.
/// The type of asset you are looking for
/// An IEnumerable off all assets paths found.
public static IEnumerable FindAssetPaths(string extension = null)
where T : Object
{
string query = BuildQueryToFindAssets(extension);
foreach (var guid in AssetDatabase.FindAssets(query))
yield return AssetDatabase.GUIDToAssetPath(guid);
}
static string BuildQueryToFindAssets(string extension = null)
where T : Object
{
string typeName = typeof(T).ToString();
int i = typeName.LastIndexOf('.');
if (i != -1)
{
typeName = typeName.Substring(i+1, typeName.Length - i-1);
}
return string.IsNullOrEmpty(extension) ? $"t:{typeName}" : $"t:{typeName} glob:\"**/*{extension}\"";
}
}
}