Added initial version 0.1.0
This commit is contained in:
parent
6aa4cb8596
commit
416759c213
64 changed files with 2181 additions and 0 deletions
67
Editor/Utilities/EditorAssetUtility.cs
Normal file
67
Editor/Utilities/EditorAssetUtility.cs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue