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. android------DDMS files not found: tools\hprof-conv.exe

    好久没有Eclipse了,使用一下就遇到坑,使用eclipse突然发生这个问题:DDMS files not found: ***\sdk\tools\hprof-conv.exe,无法连接模拟器 在 ...

  2. Binary Gap(二进制空白)

    中文标题[二进制空白] 英文描述 A binary gap within a positive integer N is any maximal sequence of consecutive zer ...

  3. scrapy 爬虫框架(一)

    一 . scrapy 的安装 安装scrapy框架时,需要先安装依赖包. #Linux: pip3 install scrapy #Windows: a. pip3 install wheel b. ...

  4. Python基础之模块以及5大模块的使用

    内容梗概: 1. 模块的简单认识 2. collections模块 3. time时间模块 4. random模块 5. os模块 6. sys模块 1.模块的简单认识定义:模块就是我们把装有特定功能 ...

  5. SQL SERVER 子查询使用Order By;按In排序

    [子查询]使用order by percent * from table order by id) a 这时发现结果没有按id排序,需要将100 percent 改成 99.999 percent 或 ...

  6. 【MySQL】【3】String和Date相互转换

    正文: 1,Date转String --结果:<2019-04-10> SELECT DATE_FORMAT(SYSDATE(), "%Y-%m-%d") FROM D ...

  7. 用vivado实现4比特加法器

    `timescale 1ns / 1ps module add_4_beha( a, b, cin, sum ); :] a; :] b; input cin; output sum; :]a; :] ...

  8. WDA基础四:Select-option的使用

    select option是方便用户和数据处理的,就是丑了点... 前面使用的input直接做查询条件有哥弊端,就是查询的时候需要判断字段是否有选择条件,然后要将选择条件做成range table.. ...

  9. sun.misc.BASE64Encoder图片编码,并在页面显示

    Data URI scheme是在RFC2397中定义的,目的是将一些小的数据,直接嵌入到网页中,从而不用再从外部文件载入.在上面的Data URI中,data表示取得数据的协定名称,image/pn ...

  10. Linux下的Nginx的配置+Tomcat启动

    Linux下的Nginx的配置 首次如果你只仅主机模式,那么请你切换到桥接模式或者net模式. 1.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc ...