【封装】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);
            }
        }
    }
}
相关推荐
babywang08 小时前
Unity笔记--AssetBundle详解
unity·游戏引擎
淡海水9 小时前
01-04-认知篇-Unity内存全景
unity·c#·游戏引擎·.net·gc
598866753@qq.com19 小时前
Codex与Unity
unity·游戏引擎
淡海水20 小时前
01-03-认知篇-C#内存模型深度解析
unity·c#·游戏引擎·.net·gc
淡海水1 天前
16-02-C#常用数据结构源码-附录B-源码索引速查
unity·c#·游戏引擎·il2cpp
警醒与鞭策2 天前
【无标题】
android·unity·性能优化·游戏引擎·perforce
zhchyun20082 天前
【Unity UI 进阶】仿 Element UI 打造企业级 Unity UI 组件库(10)
ui·unity·游戏引擎
地狱为王2 天前
Unity使用NVIDIA的Audio2Face驱动口型
unity·游戏引擎
ebiobiz2 天前
STM32F4 Unity 单元测试与多次测试失败率统计详细步骤
stm32·unity·单元测试
淡海水2 天前
16-01-C#数据结构源码-附录A-术语表
unity·c#·游戏引擎·il2cpp