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 包里的AssetName
  • deps 依赖的其他bundleName

上述过程就完成了打包,具体可以参考的github: https://github.com/aodota/TestUnity

作者:Aodota
链接:https://www.jianshu.com/p/4648a400b721
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

AssetBundle打包详解的更多相关文章

  1. VS2010开发程序打包详解

    VS2010开发程序打包详解 转自:http://blog.sina.com.cn/s/blog_473b385101019ufr.html 首先打开已经完成的工程,如图: 下面开始制作安装程序包. ...

  2. Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)

    Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编辑配置文件(pml.xml)(我 ...

  3. python 打包详解

    基本步骤: 1. 写setup.py 2. 运行“python setup.py sdist” 3. 在当前目录下会生成文件夹“dist”,打包好的代码就在dist中,以“.tar.gz”的形式被压缩 ...

  4. UDK游戏打包详解

    转自:http://blog.sina.com.cn/s/blog_944177030100ycki.html 安装完的udk目录下有4个主要的文件夹 Binaries -这个文件夹包含游戏的exe程 ...

  5. [Android Pro] Java进阶学习:jar打包详解

    jar文件听说过吗,没有?或者陌生!好,没关系,这就是我们的第一站:打包发布. 为什么会有这个玩意呢,首先,这是jar的全称:JavaTM Archive (JAR) file,是的,就是java存档 ...

  6. 使用intellJ导入非maven,gradle等非构建工程的依赖,发布工程时候的打包详解

    一.导入 1.java项目在没有导入该jar包之前,如图: 2.点击 File ->  Project Structure(快捷键 Ctrl + Alt + Shift + s),点击Proje ...

  7. web程序打包详解

       重要更新:鉴于很多小伙伴们说看不到图,我这边换了几个浏览器看了下,都看得到的,估计是网速问题,请耐心等待,另外,为了更好的方便大家学习,特此提供源码以及一个word文档,word文档就是本文内容 ...

  8. Eclipse jar打包详解

    通过Eclipse下的演示工程,介绍如何打包这样的项目:要导出的类里边用到了别的jar包. 方法/步骤     1. Eclipse下的演示工程结构如下图所示,其中Task.java是当前工程运行的M ...

  9. Android系列之Android 命令行手动编译打包详解

    Android 命令行手动编译打包过程图 [详细步骤]: 1使用aapt生成R.java类文件:  例:  E:\androidDev\android-sdk-windows2.2\tools> ...

随机推荐

  1. 微信小程序选择图片,查看图片信息,浏览图片,图片上传

    依次点击链接请查看以下步骤 选择图片: https://mp.weixin.qq.com/debug/wxadoc/dev/api/media-picture.html#wxchooseimageob ...

  2. 『MXNet』第八弹_数据处理API_下_Image IO专题

    想学习MXNet的同学建议看一看这位博主的博客,受益良多. 在本节中,我们将学习如何在MXNet中预处理和加载图像数据. 在MXNet中加载图像数据有4种方式. 使用 mx.image.imdecod ...

  3. XXE漏洞

    原理:XML外部实体注入,简称XXE漏洞,XML数据在传输中数据被修改,服务器执行被恶意插入的代码.当允许引用外部实体时,通过构造恶意内容,就可能导致任意文件读取.系统命令执行.内网端口探测.攻击内网 ...

  4. 4月22 mysql常用函数

    一.数学函数 数学函数主要用于处理数字,包括整型.浮点数等. ABS(x) 返回x的绝对值 SELECT ABS(-1) -- 返回1 CEIL(x),CEILING(x) 返回大于或等于x的最小整数 ...

  5. 根据userAgent判断打开网页的所在终端,以及浏览器

    function _IsIOS() { var ua = navigator.userAgent.toLowerCase(); if(ua.match(/iPhone\sOS/i) == " ...

  6. leetcode-algorithms-15 3Sum

    leetcode-algorithms-15 3Sum Given an array nums of n integers, are there elements a, b, c in nums su ...

  7. Spring Cloud系列之客户端请求带“Authorization”请求头,经过zuul转发后丢失了

    先摆解决方案: 方法一: 方法二: zuul.routes.<routeName>.sensitive-headers= zuul.routes.<routeName>.cus ...

  8. zzw原创_expdp及impdp中的exclude及include参数的那点事

    zzw原创:转载请注明出处 在oracle的expdp 及imdpd命令中,exclude及include参数还是有一些要注意的地方,特别是涉及选择性条件时. 一.通用 1.exclude及inclu ...

  9. vue组件生命周期详解

    Vue所有的生命周期钩子自动绑定在this上下文到实例中,因此你可以访问数据,对属性和方法进行运算.这意味着你不能使用箭头函数来定义一个生命周期方法.这是因为箭头函数绑定了父上下文,因此this与你期 ...

  10. loj 10117 简单题(cqoi 2006)

    题目来源:CQOI 2006 有一个 n 个元素的数组,每个元素初始均为 0.有 m条指令,要么让其中一段连续序列数字反转——0变 1,1 变 0(操作 1),要么询问某个元素的值(操作 2). 例如 ...