AssetBundle打包详解
Unity5.x AssetBundle打包详解
在网上查看了很多资料,想详细搞清楚AssetBundle的原理。以实现符合项目需求的打包工具和加载逻辑
1. AssetBundle是什么?
AssetBundle是Unity用于动更的一种资源打包格式,如果某个资源需要动更的话,它必须被打包成AssetBundle
2. AssetBundle打包常见要面临的问题?
- 如何组织打包
- 如何避免资源重复打包
- 打包的资源如何加载
3. 我的打包方案
Unity5.x已经大幅度简化了AssetBundl的打包过程,理论上只需要调用一个API即可以完成打包
BuildPipeline.BuildAssetBundles(RES_OUTPUT_PATH, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneOSXIntel);
但资源之间的依赖关系,避免资源重复打包的问题还是要自己解决。Unity5.x提供了一个很好的东西,就是可以给资源设置AssetBundleName

Unity5.x中会将设置AssetBundleName相同的资源打包到一起,所以我们的打包过程其实就是把需要打包到一起的资源设置成相同的AssetBundleName,为每个资源设置AssetBundleName。最后调用打包API即可完成打包
我目前使用的打包规则是:
- prefab、scene等文件单独打包
- 图片资源按文件夹打包
关键代码如下:
/// <summary>
/// 设置AssetBundleName
/// </summary>
/// <param name="fullpath">Fullpath.</param>
public static void setAssetBundleName(string fullPath)
{
string[] files = System.IO.Directory.GetFiles (fullPath);
if (files == null || files.Length == 0) {
return;
}
Debug.Log ("Set AssetBundleName Start......");
string dirBundleName = fullPath.Substring (RES_SRC_PATH.Length);
dirBundleName = dirBundleName.Replace ("/", "@") + ASSET_BUNDLE_SUFFIX;
foreach (string file in files) {
if (file.EndsWith (".meta")) {
continue;
}
AssetImporter importer = AssetImporter.GetAtPath (file);
if (importer != null) {
string ext = System.IO.Path.GetExtension (file);
string bundleName = dirBundleName;
if (null != ext && (ext.Equals (".prefab")||ext.Equals(".unity"))) {
// prefab单个文件打包
bundleName = file.Substring (RES_SRC_PATH.Length);
bundleName = bundleName.Replace ("/", "@");
if (null != ext) {
bundleName = bundleName.Replace (ext, ASSET_BUNDLE_SUFFIX);
} else {
bundleName += ASSET_BUNDLE_SUFFIX;
}
}
bundleName = bundleName.ToLower ();
Debug.LogFormat ("Set AssetName Succ, File:{0}, AssetName:{1}", file, bundleName);
importer.assetBundleName = bundleName;
EditorUtility.UnloadUnusedAssetsImmediate();
// 存储bundleInfo
AssetBuildBundleInfo info = new AssetBuildBundleInfo();
info.assetName = file;
info.fileName = file;
info.bundleName = bundleName;
if (null != ext) {
info.fileName = file.Substring (0, file.IndexOf (ext));
}
fileMap.Add (file, info);
List<AssetBuildBundleInfo> infoList = null;
bundleMap.TryGetValue(info.bundleName, out infoList);
if (null == infoList) {
infoList = new List<AssetBuildBundleInfo> ();
bundleMap.Add (info.bundleName, infoList);
}
infoList.Add (info);
} else {
Debug.LogFormat ("Set AssetName Fail, File:{0}, Msg:Importer is null", file);
}
}
Debug.Log ("Set AssetBundleName End......");
}
打包好之后生成依赖关系的配置文件,配置文件格式如下:
<files>
<file>
<bundleName>prefabs@login.unity3d</bundleName>
<fileName>Assets/Resources/Prefabs/Login</fileName>
<assetName>Assets/Resources/Prefabs/Login.prefab</assetName>
<deps>
<dep>textures@common.unity3d</dep>
<dep>textures@login.unity3d</dep>
</deps>
</file>
<file>
<bundleName>scenes@main.unity3d</bundleName>
<fileName>Assets/Resources/Scenes/Main</fileName>
<assetName>Assets/Resources/Scenes/Main.unity</assetName>
</file>
<file>
<bundleName>textures@common.unity3d</bundleName>
<fileName>Assets/Resources/Textures/Common/btn_blue</fileName>
<assetName>Assets/Resources/Textures/Common/btn_blue.png</assetName>
</file>
<file>
<bundleName>textures@common.unity3d</bundleName>
<fileName>Assets/Resources/Textures/Common/btn_red</fileName>
<assetName>Assets/Resources/Textures/Common/btn_red.png</assetName>
</file>
<file>
<bundleName>textures@login.unity3d</bundleName>
<fileName>Assets/Resources/Textures/Login/bg</fileName>
<assetName>Assets/Resources/Textures/Login/bg.png</assetName>
</file>
<file>
<bundleName>textures@login.unity3d</bundleName>
<fileName>Assets/Resources/Textures/Login/text_input</fileName>
<assetName>Assets/Resources/Textures/Login/text_input.png</assetName>
</file>
</files>
bundleName
打包的文件名fileName
包里包含的文件名assetName
包里的AssetNamedeps
依赖的其他bundleName
上述过程就完成了打包,具体可以参考的github: https://github.com/aodota/TestUnity
作者:Aodota
链接:https://www.jianshu.com/p/4648a400b721
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
AssetBundle打包详解的更多相关文章
- VS2010开发程序打包详解
VS2010开发程序打包详解 转自:http://blog.sina.com.cn/s/blog_473b385101019ufr.html 首先打开已经完成的工程,如图: 下面开始制作安装程序包. ...
- Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)
Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编辑配置文件(pml.xml)(我 ...
- python 打包详解
基本步骤: 1. 写setup.py 2. 运行“python setup.py sdist” 3. 在当前目录下会生成文件夹“dist”,打包好的代码就在dist中,以“.tar.gz”的形式被压缩 ...
- UDK游戏打包详解
转自:http://blog.sina.com.cn/s/blog_944177030100ycki.html 安装完的udk目录下有4个主要的文件夹 Binaries -这个文件夹包含游戏的exe程 ...
- [Android Pro] Java进阶学习:jar打包详解
jar文件听说过吗,没有?或者陌生!好,没关系,这就是我们的第一站:打包发布. 为什么会有这个玩意呢,首先,这是jar的全称:JavaTM Archive (JAR) file,是的,就是java存档 ...
- 使用intellJ导入非maven,gradle等非构建工程的依赖,发布工程时候的打包详解
一.导入 1.java项目在没有导入该jar包之前,如图: 2.点击 File -> Project Structure(快捷键 Ctrl + Alt + Shift + s),点击Proje ...
- web程序打包详解
重要更新:鉴于很多小伙伴们说看不到图,我这边换了几个浏览器看了下,都看得到的,估计是网速问题,请耐心等待,另外,为了更好的方便大家学习,特此提供源码以及一个word文档,word文档就是本文内容 ...
- Eclipse jar打包详解
通过Eclipse下的演示工程,介绍如何打包这样的项目:要导出的类里边用到了别的jar包. 方法/步骤 1. Eclipse下的演示工程结构如下图所示,其中Task.java是当前工程运行的M ...
- Android系列之Android 命令行手动编译打包详解
Android 命令行手动编译打包过程图 [详细步骤]: 1使用aapt生成R.java类文件: 例: E:\androidDev\android-sdk-windows2.2\tools> ...
随机推荐
- apply、call
call(),apply() 1.每个函数都包含两个非继承而来的方法:call()和apply() 2.在特定的作用域内调用函数,等于设置函数体内的this对象,以扩充函数赖以运行的作用域 3.app ...
- Creating a Hadoop-2.x project in Eclipse
Creating a Hadoop-2.x project in Eclipse hortonworks:MapReduce Ports http://docs.hortonworks.com/HDP ...
- 廖雪峰网站:学习python基础知识(一)
1. python能做什么? 可以做日常任务,比如自动备份你的MP3:可以做网站,很多著名的网站包括YouTube就是Python写的:可以做网络游戏的后台,很多在线游戏的后台都是Python开发的. ...
- leetcode-algorithms-22 Generate Parentheses
leetcode-algorithms-22 Generate Parentheses Given n pairs of parentheses, write a function to genera ...
- leetcode-algorithms-3 Longest Substring Without Repeating Characters
leetcode-algorithms-3 Longest Substring Without Repeating Characters Given a string, find the length ...
- Spring + Mybatis项目实现数据库读写分离
主要思路:通过实现AbstractRoutingDataSource类来动态管理数据源,利用面向切面思维,每一次进入service方法前,选择数据源. 1.首先pom.xml中添加aspect依赖 & ...
- 安卓——Handler延迟跳转
//声明控制对象 Handler handler =new Handler(){ @Override public void handleMessage(Message msg) { super.ha ...
- docker实战系列之docker 端口映射错误解决方法
错误: Error response from daemon: Cannot start container web: iptables failed: iptables -t nat -A DOCK ...
- Python3+ssl实现加密通信
一.说明 1. python标准库ssl可实现加密通信 2. ssl库底层使用openssl,做了面向对像化改造和简化,但还是可以明显看出openssl的痕迹 3. 本文先给出python实现的soc ...
- WordDenified.exportedUI
<mso:cmd app="Word" dt="1" /><mso:customUI xmlns:x1="xuzaAzWord&qu ...