Skip to content

Instantly share code, notes, and snippets.

@LuviKunG
Last active May 19, 2024 15:10
Show Gist options
  • Save LuviKunG/e2ede9692a41896a0d764bae3d88a5b4 to your computer and use it in GitHub Desktop.
Save LuviKunG/e2ede9692a41896a0d764bae3d88a5b4 to your computer and use it in GitHub Desktop.
Unity Editor static class for asset database operations.
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace LuviKunG.Editor
{
using UnityObject = UnityEngine.Object;
/// <summary>
/// Helper class for asset database operations.
/// </summary>
public static class AssetDatabaseHelper
{
/// <summary>
/// Find assets of a specific type.
/// </summary>
/// <param name="type">The type of asset to find.</param>
/// <param name="paths">The paths to search for assets.</param>
/// <returns>A list of assets of type T.</returns>
public static List<UnityObject> FindAssets(Type type, params string[] paths)
{
List<UnityObject> assets = new();
if (type.IsSubclassOf(typeof(Component)))
{
string[] assetGuids = AssetDatabase.FindAssets("t:prefab", paths);
for (int i = 0; i < assetGuids.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(assetGuids[i]);
GameObject gameObject = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (gameObject != null)
{
Component component = gameObject.GetComponent(type);
if (component != null)
{
assets.Add(component);
}
}
}
}
else if (type.IsSubclassOf(typeof(ScriptableObject)))
{
string[] assetGuids = AssetDatabase.FindAssets("t:" + type.Name, paths);
for (int i = 0; i < assetGuids.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(assetGuids[i]);
UnityObject asset = AssetDatabase.LoadAssetAtPath(path, type);
if (asset != null)
{
assets.Add(asset);
}
}
}
else if (type.IsSubclassOf(typeof(UnityObject)))
{
string[] assetGuids = AssetDatabase.FindAssets("t:" + type.Name, paths);
for (int i = 0; i < assetGuids.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(assetGuids[i]);
UnityObject asset = AssetDatabase.LoadAssetAtPath(path, type);
if (asset != null)
{
assets.Add(asset);
}
}
}
return assets;
}
/// <summary>
/// Find assets of a specific type.
/// </summary>
/// <typeparam name="T">The type of asset to find.</typeparam>
/// <param name="paths">The paths to search for assets.</param>
/// <returns>A list of assets of type T.</returns>
public static List<T> FindAssets<T>(params string[] paths) where T : UnityObject
{
List<T> assets = new();
if (typeof(T).IsSubclassOf(typeof(Component)))
{
string[] assetGuids = AssetDatabase.FindAssets("t:prefab", paths);
for (int i = 0; i < assetGuids.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(assetGuids[i]);
GameObject gameObject = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (gameObject != null)
{
T component = gameObject.GetComponent<T>();
if (component != null)
{
assets.Add(component);
}
}
}
}
else if (typeof(T).IsSubclassOf(typeof(ScriptableObject)))
{
string[] assetGuids = AssetDatabase.FindAssets("t:" + typeof(T).Name, paths);
for (int i = 0; i < assetGuids.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(assetGuids[i]);
T asset = AssetDatabase.LoadAssetAtPath<T>(path);
if (asset != null)
{
assets.Add(asset);
}
}
}
else if (typeof(T).IsSubclassOf(typeof(UnityObject)))
{
string[] assetGuids = AssetDatabase.FindAssets("t:" + typeof(T).Name, paths);
for (int i = 0; i < assetGuids.Length; i++)
{
string path = AssetDatabase.GUIDToAssetPath(assetGuids[i]);
T asset = AssetDatabase.LoadAssetAtPath<T>(path);
if (asset != null)
{
assets.Add(asset);
}
}
}
return assets;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment