67 lines
No EOL
2.2 KiB
C#
67 lines
No EOL
2.2 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
namespace Module.ProjectValidator.Editor
|
|
{
|
|
internal static class EditorAssetUtility
|
|
{
|
|
public static T LoadFirstAsset<T>(string name) where T : Object
|
|
{
|
|
var guids = AssetDatabase.FindAssetGUIDs($"a:assets t:{typeof(T).Name} {name}");
|
|
return guids.Length != 0 ? AssetDatabase.LoadAssetByGUID<T>(guids[0]) : null;
|
|
}
|
|
|
|
public static T[] LoadAllAssets<T>() where T : Object
|
|
{
|
|
var guids = AssetDatabase.FindAssetGUIDs($"a:assets t:{typeof(T).Name}");
|
|
var list = new List<T>(guids.Length);
|
|
|
|
foreach (var guid in guids)
|
|
{
|
|
var asset = AssetDatabase.LoadAssetByGUID<T>(guid);
|
|
|
|
if (asset != null)
|
|
list.Add(asset);
|
|
}
|
|
|
|
return list.ToArray();
|
|
}
|
|
|
|
internal static GUID GetAssetGuid(Object obj)
|
|
{
|
|
var assetGuid = new GUID();
|
|
|
|
if (obj is GameObject gameObject)
|
|
{
|
|
if (gameObject.scene.isLoaded)
|
|
GUID.TryParse(AssetDatabase.AssetPathToGUID(gameObject.scene.path), out assetGuid);
|
|
else if (PrefabUtility.IsPartOfPrefabAsset(gameObject))
|
|
GUID.TryParse(AssetDatabase.AssetPathToGUID(gameObject.scene.path), out assetGuid);
|
|
}
|
|
else
|
|
{
|
|
GUID.TryParse(AssetDatabase.GetAssetPath(obj), out assetGuid);
|
|
}
|
|
|
|
return assetGuid;
|
|
}
|
|
|
|
internal static string GetAssetName(GUID assetGuid)
|
|
{
|
|
if (assetGuid.Empty())
|
|
return string.Empty;
|
|
|
|
var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
|
|
return Path.GetFileNameWithoutExtension(assetPath);
|
|
}
|
|
|
|
internal static GUID ObjectToAssetGuid(Object obj)
|
|
{
|
|
var assetPath = AssetDatabase.GetAssetPath(obj);
|
|
var strGuid = AssetDatabase.AssetPathToGUID(assetPath);
|
|
return GUID.TryParse(strGuid, out var guid) ? guid : new GUID();
|
|
}
|
|
}
|
|
} |