【封装】Unity编辑器模式GUID加载资源

介绍

在编辑器模式下通过GUID 获取工程目录下的指定资源的接口工具封装

工具原理
  • 借助AssetDatabaseAPI
    • FindAssets : 获取 GUID
    • GUIDToAssetPath : 通过GUID获取路径
    • LoadAssetAtPath<T>: 通过路径加载资源
代码:
csharp 复制代码
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);
            }
        }
    }
}
相关推荐
雪儿waii19 分钟前
Unity 中的 Resources 详解
unity·游戏引擎
RReality11 小时前
【Unity UGUI】Toggle / ToggleGroup 与 Dropdown
ui·unity·游戏引擎·图形渲染·材质
雪儿waii12 小时前
Unity 中的 InvokeRepeating 详解
unity·游戏引擎
mxwin12 小时前
Unity Shader 程序化生成:Shader 中的数学宇宙
unity·游戏引擎
雪儿waii14 小时前
Unity 中的 Quaternion(四元数)详解
unity·游戏引擎
RReality14 小时前
【Unity UGUI】ScrollRect 与 Scrollbar 深度用法
unity·游戏引擎
人邮异步社区14 小时前
如何自学游戏引擎的开发?
unity·程序员·游戏引擎
郝学胜-神的一滴16 小时前
[简化版 Games 101] 计算机图形学 05:二维变换下
c++·unity·图形渲染·three.js·opengl·unreal
mxwin1 天前
Unity URP 热更新兼容性:Shader 在 IL2CPP 打包下的注意事项
unity·游戏引擎
mxwin1 天前
Unity shader中TransformWorldToShadowCoord原理解析
unity·游戏引擎·shader