介绍
在编辑器模式下通过GUID获取工程目录下的指定资源的接口工具封装
工具原理
- 借助
AssetDatabase
APIFindAssets
: 获取 GUIDGUIDToAssetPath
: 通过GUID获取路径LoadAssetAtPath<T>
: 通过路径加载资源
代码:
public static class GetAssetUtil
{public static void GetAsset<T>(String[] folderPaths,Action<T> action) where T : UnityEngine.Object{var guids = AssetDatabase.FindAssets($"t:{typeof(T).Name}",folderPaths);foreach (var guid in guids){var path = AssetDatabase.GUIDToAssetPath(guid);var asset = AssetDatabase.LoadAssetAtPath<T>(path);if (asset!= null){action(asset);}}}
}