【封装】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);
            }
        }
    }
}
相关推荐
我写代码菜如坤5 小时前
unity获取键盘按键
unity·计算机外设·游戏引擎
雪 狼12 小时前
unity 使用UnityWebRequest从服务器下载
服务器·unity·游戏引擎
Unity打怪升级19 小时前
Unity入门之重要组件和API(3) : Transform
unity·c#·游戏引擎
Unity游戏开发1 天前
【Unity navigation面板】
unity·游戏引擎
程序员正茂1 天前
Unity游戏帧率查看软件Fraps
unity·帧率
Unity游戏开发1 天前
【Unity navmeshaggent 组件】
unity·游戏引擎
陈言必行2 天前
Unity 资源 之 Sweet Cakes Icon套装,110个高品质蛋糕图标分享
unity·游戏引擎
唐小墨同学2 天前
机械拆装-基于Unity-装配功能的实现
unity·游戏引擎
向宇it2 天前
【unity实战】使用unity的新输入系统InputSystem+有限状态机设计一个玩家状态机控制——实现玩家的待机 移动 闪避 连击 受击 死亡状态切换
开发语言·游戏·unity·游戏引擎
ZKY_242 天前
【APK】Unity出android包,报错 Gradle build failed.See the Console for details
unity