Unity Editor 打包指定资源(AssetBundle)和加载指定资源

news/2024/7/3 17:57:35

前言:

        一般用于ui资源打包和加载,代码比较简单没什么好说的,直接上代码。

        打包代码:

    [MenuItem("Assets/打包指定的预设")]
    public static void BuildAsset() {
        var selectObject = Selection.activeObject;
        if (selectObject != null) {
            if (selectObject is GameObject go) {
                try {
                    EditorUtility.DisplayProgressBar("打包资源", go.name, 1.0f);
                    AssetBundleBuild build = new AssetBundleBuild();

                    var path = Application.streamingAssetsPath + "/assetbundle";
                    if (!Directory.Exists(path)) {
                        Directory.CreateDirectory(path);
                    }

                    build.assetBundleName = go.name;
                    build.assetNames = new string[] { AssetDatabase.GetAssetPath(selectObject) };
                    AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles(path,
                        new AssetBundleBuild[] { build },
                        BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);
                    File.Delete(path + "/" + go.name + ".manifest");
                    AssetDatabase.Refresh();
                }
                catch (Exception e) {
                    Debug.Log($"打包资源:{go.name}失败,Error:{e.Message}");
                }

                EditorUtility.ClearProgressBar();
            }
        }


        //string inputPath = EditorUtility.OpenFolderPanel("选择编译文件夹",
        //      UnityEngine.Application.dataPath, "HostScripts");
        //if (string.IsNullOrEmpty(inputPath)) {
        //    EditorUtility.DisplayDialog("错误", "必须选择文件夹才能进行编译", "确定");
        //}
    }

加载代码:

    public void LoadUIformAB<T>(string path, string assetName, Action<T> callBack) where T : UnityEngine.Object {
        AssetBundle ab = null;
        if (abUIMap.TryGetValue(path, out ab)) {
            try {
                T asset = (T)GameObject.Instantiate(ab.LoadAsset(assetName));
                callBack?.Invoke(asset);
            }
            catch (Exception e) {
                McLogger.Error("UI", nameof(UISetting), $"加载UIab包报错{e.Message}---->{e.StackTrace}");
                callBack?.Invoke(null);
            }
        }
        else {
            if (File.Exists(path)) {
                try {
                    ab = AssetBundle.LoadFromFile(path);
                    abUIMap.Add(path, ab);
                    T asset = (T)GameObject.Instantiate(ab.LoadAsset(assetName));
                    callBack?.Invoke(asset);
                }
                catch (Exception e) {
                    McLogger.Error("UI", nameof(UISetting), $"加载UIab包报错{e.Message}---->{e.StackTrace}");
                    callBack?.Invoke(null);
                }
            }
            else {
                callBack?.Invoke(null);
            }
        }
    }
    private string path = Application.streamingAssetsPath + $"/assetbundle/sequenceframe";//ab包路径

    private void LoadAssetBundleFile(){
        SimApp.UIRuntime.LoadUIformAB<GameObject>(path, "sequenceframe", (data) =>
        {
            if (data != null)
            {
                data.transform.SetParent(transform);
                data.GetComponent<RectTransform>().localPosition = Vector3.zero;
                data.GetComponent<RectTransform>().sizeDelta = Vector2.zero;
                data.transform.localScale = Vector3.one;
            }
        });
    }


http://lihuaxi.xjx100.cn/news/1698430.html

相关文章

软件研发流程、架构规范、技术标准、需求过程等全文档

前言&#xff1a; 软件项目管理全文档包括以下几个方面&#xff1a;需求分析、项目规划、过程管理、测试和部署。 全文档获取&#xff1a;Q:262086839 例图在文末。 正文&#xff1a; 一、需求分析是软件项目管理的第一步&#xff0c;也是非常关键的一步。在需求分析阶段&…

Ubuntu桌面环境的切换方法

你在找它吗&#xff1f; 国内麒麟、深度等系统虽然界面更炫&#xff0c;但——软件仓库与Ubuntu官方已不兼容。国内系统遇到稳定性问题&#xff0c;还是得拿Ubuntu做参照。今天本来介绍下这款Linux桌面。 为什么在 Ubuntu 上考虑 LXQt&#xff1f; 性能&#xff1a;LXQt设计为…

标准模板库--STL

这里写目录标题 STL初始STL诞生STL基本概念STL六大组件STL中的容器、算法、迭代器容器算法迭代器初识vector存放内置数据类型vector存放自定义数据类型vector中嵌套vector string容器构造函数赋值操作字符串拼接字符串查找与替换字符串比较单字符存取&#xff08;读写&#xff…

UOS系统无法开机问题解决

1、问题截图1 &#xff1a; 2、问题截图2 &#xff1a; 3、问题截图3&#xff1a; 解决方案&#xff1a; 修复磁盘无效&#xff0c;建议拷贝数据&#xff0c;还原系统 请先拷贝数据 进live模式在root a或root b 或系统盘找到Home文件夹里-找到用户名-里面就是用户的数据&am…

极米投影仪怎么样?轻薄投影极米Z7X值得选择吗?

对于当代快节奏的打工人而言&#xff0c;我们有时候很需要一些独特的方式来逃离日常生活的疲惫&#xff0c;拥有一个自己的空间&#xff0c;享受一个人的独处时光。要想享受居家独处好时光&#xff0c;当然少不了家居娱乐好伙伴极米Z7X。无论你是想要看一场科幻大片&#xff0c…

map和set底层实现【C++】

文章目录 map和set模板参数红黑树结点中的数据模板参数中的仿函数正向迭代器运算符重载--运算符重载 库里的写法setmapRBTree map和set模板参数 set是K模型的容器&#xff0c;而map是KV模型的容器 如何用一棵KV模型的红黑树同时实现map和set template<class K ,class V>…

FPGA片上RAM、片上ROM Nios 程序不起作用的解决方法

ctrl B 编译Nios工程 将 Nios software 的 meminit.qip onchip_rom.hex onchip_ram.hex meminit.spd 文件拷贝到FPGA目录下&#xff0c;再编译FPGA能起作用 Nios设置&#xff1a; reset 设置为 ROM 异常设置为 RAM 无优化

直接在网页上获取【微信公众号】用户的【openId】

一、公众号openId是什么&#xff1f; 公众号openId是用户在当前公众号下的唯一标识&#xff08;‘身份证’&#xff09;&#xff0c;就是说通过这个openId&#xff0c;就能区分在这个公众号下具体是哪个用户。 获取微信公众号用户的OpenId&#xff08;用户关注公众号后&#x…