module-project-validator/Editor/Utilities/EditorAssetUtility.cs
Anders Ejlersen dd55a87740 - Validator: Added asset validators with material texture and shader validation
- Validator: Added option to enable/disable certain validators
- Project Settings: Fixed issue, where changes weren't always saved
- Unity: Removed deprecated warnings in Unity 6.4
2026-05-24 18:06:56 +02:00

86 lines
No EOL
2.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Module.ProjectValidator.Editor
{
internal static class EditorAssetUtility
{
public static T LoadFirstAsset<T>(string name) where T : Object
{
var guids = AssetDatabase.FindAssetGUIDs($"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();
}
public static Object[] LoadAllAssets(Type type)
{
var guids = AssetDatabase.FindAssetGUIDs($"a:assets t:{type.Name}");
var list = new List<Object>(guids.Length);
foreach (var guid in guids)
{
var asset = AssetDatabase.LoadAssetByGUID(guid, type);
if (asset != null)
list.Add(asset);
}
return list.ToArray();
}
internal static GUID GetAssetGuid(Object obj)
{
var assetPath = string.Empty;
if (obj is GameObject gameObject)
{
if (gameObject.scene.isLoaded)
assetPath = gameObject.scene.path;
else if (PrefabUtility.IsPartOfPrefabAsset(gameObject))
assetPath = AssetDatabase.GetAssetPath(gameObject);
}
else
{
assetPath = AssetDatabase.GetAssetPath(obj);
}
GUID.TryParse(AssetDatabase.AssetPathToGUID(assetPath), out var 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();
}
}
}