Unity 简单打包脚本

打包脚本

这个打包脚本适用于做demo,脚本放在Editor目录下

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;public class BuildAB
{[MenuItem("Tools/递归遍历文件夹下的资源并打包")]public static void BuildDirectory(){//需要打包的文件夹路径string path = Application.dataPath + "/Res";List<string> list = new List<string>();GetFilePath(path, ref list);List<AssetBundleBuild> mult = new List<AssetBundleBuild>();for (int i = 0; i < list.Count; i++){string assetName = list[i].Replace(Application.dataPath, "Assets").Replace("\\", "/");string bundleName = Path.GetFileNameWithoutExtension(assetName);AssetBundleBuild ab = new AssetBundleBuild();ab.assetBundleName = bundleName;//assetNames记录bundle中所有资源的相对路径ab.assetNames = new string[] { assetName };mult.Add(ab);Debug.Log($"{assetName} {bundleName}");}string targetPath = Application.streamingAssetsPath;if (Directory.Exists(targetPath))Directory.Delete(targetPath, true);Directory.CreateDirectory(targetPath);BuildPipeline.BuildAssetBundles(targetPath, mult.ToArray(),BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);AssetDatabase.Refresh();}[MenuItem("Tools/遍历文件夹下的资源,处理依赖并打包")]public static void BuildDirectoryDependence(){string path = Application.dataPath + "/Res";List<string> list = new List<string>();GetFilePath(path, ref list);//key 资源路径,value 引用计数Dictionary<string, int> abDict = new Dictionary<string, int>();for (int i = 0; i < list.Count; i++){string assetName = list[i].Replace(Application.dataPath, "Assets").Replace("\\", "/");//获取依赖资源,返回相对路径string[] dependences = AssetDatabase.GetDependencies(assetName);for(int j = 0; j < dependences.Length; ++j){if (dependences[j].EndsWith(".cs")){continue;}Debug.Log($"{assetName} 依赖 {j} => {dependences[j]}");if (abDict.ContainsKey(dependences[j])){abDict[dependences[j]]++;}else{abDict.Add(dependences[j], 1);}}}var enumerator = abDict.GetEnumerator();List<AssetBundleBuild> mult = new List<AssetBundleBuild>();while (enumerator.MoveNext()){string fileName = enumerator.Current.Key;int refCount = enumerator.Current.Value;string bundleName = Path.GetFileNameWithoutExtension(fileName);if(refCount > 1){//引用计数大于1的为公共资源,单独打包bundleName = "common/" + bundleName;}AssetBundleBuild ab = new AssetBundleBuild();ab.assetBundleName = bundleName;//assetNames记录bundle中所有资源的相对路径ab.assetNames = new string[] { fileName };mult.Add(ab);}string targetPath = Application.streamingAssetsPath;if (Directory.Exists(targetPath))Directory.Delete(targetPath, true);Directory.CreateDirectory(targetPath);BuildPipeline.BuildAssetBundles(targetPath, mult.ToArray(),BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);AssetDatabase.Refresh();}/// <summary>/// 递归获取所有文件路径/// </summary>public static void GetFilePath(string path, ref List<string> list){string[] files = Directory.GetFiles(path);for (int i = 0; i < files.Length; i++){if (files[i].EndsWith(".cs") || files[i].EndsWith(".meta")){continue;}list.Add(files[i]);}string[] dirs = Directory.GetDirectories(path);for(int i = 0; i < dirs.Length; i++){GetFilePath(dirs[i], ref list);}}
}

在这里插入图片描述
前面的选项不会处理依赖关系,公共资源会打进不同的bundle里
后面的选项会处理依赖资源,引用计数大于1的认为是公共资源,单独打包到common文件夹

加载脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class LoadAB : MonoBehaviour
{private AssetBundleManifest _manifest;private List<string> _abList = new List<string>();private Dictionary<string, GameObject> _assetDict = new Dictionary<string, GameObject>();void Start(){LoadManifest();LoadGameObject("cube");}void LoadManifest(){AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/StreamingAssets");_manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");}private void LoadGameObject(string abName){if (string.IsNullOrEmpty(abName))return;abName = abName.ToLower();//先加载依赖资源string[] dependens = _manifest.GetAllDependencies(abName);for (int i = 0; i < dependens.Length; ++i){if (!_abList.Contains(dependens[i])){AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + dependens[i]);_abList.Add(dependens[i]);}}if (!_abList.Contains(abName)){AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abName);_abList.Add(abName);_assetDict.Add(abName, ab.LoadAsset<GameObject>(abName));}if (_assetDict.ContainsKey(abName)){GameObject.Instantiate(_assetDict[abName]);}}
}

参考

untiy AssetBundle 依赖关系树

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/193313.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

K210开发板之VSCode开发环境使用中添加或删除文件(编译失败时)需要注意事项

在最初开始接触&#xff0c;将VScode和编译环境搭载好后&#xff0c;就开始运行第一个程序了&#xff0c;为了后续方便开发测试&#xff0c;这里我自己对照官方提供的例子&#xff0c;自己调试&#xff0c;写了一个简单的文件系统 后续&#xff0c;所有关于开发的源文件都在...…

SSM框架(六):SpringBoot技术及整合SSM

文章目录 一、概述1.1 简介1.2 起步依赖1.3 入门案例1.4 快速启动 二、基础配置2.1 三种配置文件方式2.2 yaml文件格式2.3 yaml读取数据方式&#xff08;3种&#xff09; 三、多环境开发3.1 yml文件-多环境开发3.2 properties文件-多环境开发3.3 多环境命令行启动参数设置3.4 多…

【LeetCode】每日一题 2023_12_3 可获得的最大点数(前缀和/滑动窗口/贪心)

文章目录 刷题前唠嗑题目&#xff1a;可获得的最大点数题目描述代码与解题思路 结语 刷题前唠嗑 LeetCode&#xff1f;启动&#xff01;&#xff01;&#xff01; 题目&#xff1a;可获得的最大点数 题目链接&#xff1a;1423. 可获得的最大点数 题目描述 代码与解题思路 …

【Springboot+vue】如何运行springboot+vue项目

从github 或者 gitee 下载源码后&#xff0c;解压&#xff0c;再从idea打开项目 后端代码处理 这是我在gitee下载下来的源码 打开之后&#xff0c;先处理后端代码 该配置的配置&#xff0c;该部署的部署 比如将sql文件导入数据库 然后去配置文件更改配置 然后启动项目 确保…

企业网盘最新评测:哪个最好用?实用性对比与推荐

无论哪个行业&#xff0c;都离不开文件协作。因此企业网盘凭借其便捷的服务&#xff0c;强大的文件协作功能一跃成为了当下热门的办公软件之一。市面上涌现了大批企业网盘产品&#xff0c;哪个企业网盘最好用呢&#xff1f;本文就目前市面上最火的几款企业网盘产品进行测评&…

《异常检测——从经典算法到深度学习》24 用于单变量时间序列异常检测的端到端基准套件

《异常检测——从经典算法到深度学习》 0 概论1 基于隔离森林的异常检测算法 2 基于LOF的异常检测算法3 基于One-Class SVM的异常检测算法4 基于高斯概率密度异常检测算法5 Opprentice——异常检测经典算法最终篇6 基于重构概率的 VAE 异常检测7 基于条件VAE异常检测8 Donut: …

视频生成的发展史及其原理解析:从Gen2、Emu Video到PixelDance、SVD、Pika 1.0

前言 考虑到文生视频开始爆发&#xff0c;比如11月份就是文生视频最火爆的一个月 11月3日&#xff0c;Runway的Gen-2发布里程碑式更新&#xff0c;支持4K超逼真的清晰度作品(runway是Stable Diffusion最早版本的开发商&#xff0c;Stability AI则开发的SD后续版本)11月16日&a…

2023软件测试大赛总结

2023软件测试大赛总结 文章目录 2023软件测试大赛总结软件下载方式比赛方式个人总结断言使用java基础 预选赛省赛国赛 软件下载方式 进入官网下载插件&#xff08;直接下载一个完整的Eclipse就可以,这样比较方便&#xff09; 需要保证jdk版本和要求的一致&#xff0c;不然可能…

idea不需安装插件,自动生成mybatis-plus对应的实体类entity,带注解@TableName、@TableId、@TableField

目录 1、修改Generate poJOs.groovy文件 2、idea中连接数据库 3、生成entity代码 4、查看生成的实体类 1、修改Generate poJOs.groovy文件 在项目下方点击Scratches and Consoles→ Extensions→ Database Tools and SQL箭头→schema→ Generate POJOs.groovy 替换为以下文…

【前沿技术】扩散模型是什么

0. 前言 扩散模型的灵感来自非平衡热力学。他们定义了一个马尔可夫扩散步骤链&#xff0c;以缓慢地将随机噪声添加到数据中&#xff0c;然后学习逆转扩散过程以从噪声中构建所需的数据样本。与VAE或流动模型不同&#xff0c;扩散模型是通过固定程序学习的&#xff0c;并且潜在变…

十大经典系统架构设计面试题

十大经典系统架构设计面试题_架构_程序员石磊_InfoQ写作社区翻译自&#xff1a;https://medium.com/geekculture/top-10-system-design-interview-questions-10f7b5ea123d在我作为微软和Facebhttps://xie.infoq.cn/article/4c0c9328a725a76922f6547ad 任何 SDI 问题的提示 通过…

EasyMetagenome易宏基因组——简单易用的宏基因组分析流程-来自刘永鑫团队的秘密武器

原仓库地址如下&#xff0c;github有时候无法访问&#xff0c;等一段时间再试就行&#xff1a; YongxinLiu/EasyMetagenome: Easy Metagenome Pipeline (github.com) 相关文章&#xff0c;看文章更清晰这个可干啥&#xff1a; EasyAmplicon: An easy‐to‐use, open‐source…

深入了解汉字转拼音转换工具:原理与应用

一、引言 汉字作为世界上最古老、最具象形意的文字之一&#xff0c;承载了数千年的历史文明。然而&#xff0c;在现代信息技术环境下&#xff0c;汉字的输入、输出和检索等方面存在一定的局限性。拼音作为汉字的一种音标表达方式&#xff0c;能够有效地解决这些问题。本文将为…

C++:C++11新特性--lambda表达式和包装器

文章目录 lambda表达式lambda表达式的使用规则lambda表达式的用法lambda表达式的理解函数对象和lambda表达式 包装器bind lambda表达式 首先介绍什么是lambda表达式&#xff0c;在介绍这个情景前&#xff0c;可以回忆一下算法库中的sort排序&#xff1a; // lambda表达式 voi…

Git 标签管理

前言 标签 tag&#xff0c;就相当于对 某一次的 commit 做一个标识&#xff0c;起了一个别名&#xff0c;例如&#xff1a;在某个项目发布版本的时候&#xff0c;可针对最后一次 commit 起一个别名 v1.0 来标识这一次的commit。tag 的作用&#xff1a;commit id 相对于 tag 是很…

机械专业个人简历17篇

以下简历内容以机械专业相关岗位招聘需求为背景&#xff0c;我们整理了17篇且具有参考价值的简历案例&#xff0c;大家可以灵活借鉴&#xff0c;助理大家在众多候选人中脱颖而出。 机械专业简历模板下载&#xff08;可在线编辑制作&#xff09;&#xff1a;来幻主简历&#xf…

GEE:均值滤波

作者:CSDN @ _养乐多_ 本文将介绍在 Google Earth Engine(GEE)平台上,进行均值滤波操作的代码框架、核心函数和多种卷积核。 并分别以林地区域和农田区域为试验区,以NDVI图像为例。结果如下图所示, 文章目录 一、均值滤波二、完整代码三、代码链接一、均值滤波 均值滤…

CTF-PWN-堆-【malloc和free的工作流程】

文章目录 关于ptmalloc的思考缓存思想 chunk结构large bin补充fast bin 补充unsorted bin 补充top chunk 补充mmaped chunk补充Last remainder补充last remainder的产生 malloc_state补充mmap收缩阈值mmap分配阈值ptmalloc响应用户内存分配要求工作流程free时工作流程 大佬的关…

【Delphi】实现彩色日志显示框

目录 一、前言 二、实现方法 1. 第一步 2. 第二步 3. 第三步 三、主程序代码 四、下载 1. 可执行程序 2. 程序源代码 一、前言 在用Delphi做日常开发的时候&#xff0c;经常需要显示程序运行的日志&#xff0c;一般我们会使用TMemo&#xff0c;使用起来简单&#xff0c…

ElementPlus中 使用ElLoading.service, spinner: ‘el-icon-loading‘不生效

let downloadLoadingInstance ElLoading.service({ text: "正在下载数据&#xff0c;请稍候",spinner: el-icon-loading, background: "rgba(0, 0, 0, 0.7)", })使用以上代码时&#xff0c;加载的圆圈出不来&#xff0c;使用f12查看&#xff0c;即使能出…