【封装】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);
            }
        }
    }
}
相关推荐
zhchyun20087 小时前
# 【Unity UI 进阶】仿 Element UI 打造企业级 Unity UI 组件库(03)
unity
淡海水20 小时前
15-02-YooAsset面试篇-Unity架构设计与源码
java·unity·面试·c#·游戏引擎·yooasset
郝学胜-神的一滴1 天前
中级OpenGL教程 030:gl_FragCoord解锁区域渲染与深度可视化新姿势
c++·unity·游戏引擎·图形渲染·unreal engine·opengl
ellis19701 天前
u3d插件xLua[四]Lua访问C#源码分析,反射,生成代码
unity
2401_894915531 天前
GEO 定位优化源码搭建常见报错排查:数据库、伪静态、接口调试
java·数据库·网络协议·tcp/ip·spring·unity
xcLeigh2 天前
Unity基础:C#脚本的创建与挂载——让你的游戏物体活起来
游戏·unity·c#·教程
DaLiangChen3 天前
Vuforia 地面识别案例
unity·ar