using System.Collections.Generic;
using System.Linq;
using UnityEditor;
namespace AutoGroupGenerator
{
///
/// Helpers for querying the Unity asset database.
///
public static class AssetDatabaseUtil
{
///
/// Finds and loads assets of the specified type.
///
/// Asset type to search for.
/// Loaded assets of the requested type.
public static List FindAssetsOfType() where T : UnityEngine.Object
{
var guids = FindAssetGuidsForType();
var assets = new List(guids.Length);
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
T asset = AssetDatabase.LoadAssetAtPath(path);
if (asset != null)
{
assets.Add(asset);
}
}
return assets;
}
///
/// Finds asset paths for assets of the specified type.
///
/// Asset type to search for.
/// Project-relative asset paths.
public static List FindAssetPathsForType() where T : UnityEngine.Object
{
var guids = FindAssetGuidsForType();
return guids.Select(AssetDatabase.GUIDToAssetPath).ToList();
}
///
/// Finds asset GUIDs for assets of the specified type.
///
/// Asset type to search for.
/// Array of GUID strings.
public static string[] FindAssetGuidsForType() where T : UnityEngine.Object
{
return AssetDatabase.FindAssets($"t:{typeof(T).Name}");
}
}
}