Unity5.x AssetBundle打包详解

在网上查看了很多资料,想详细搞清楚AssetBundle的原理。以实现符合项目需求的打包工具和加载逻辑

1. AssetBundle是什么?

AssetBundle是Unity用于动更的一种资源打包格式,如果某个资源需要动更的话,它必须被打包成AssetBundle

2. AssetBundle打包常见要面临的问题?

  • 如何组织打包
  • 如何避免资源重复打包
  • 打包的资源如何加载

3. 我的打包方案

Unity5.x已经大幅度简化了AssetBundl的打包过程,理论上只需要调用一个API即可以完成打包

  1. BuildPipeline.BuildAssetBundles(RES_OUTPUT_PATH, BuildAssetBundleOptions.DeterministicAssetBundle, BuildTarget.StandaloneOSXIntel);

但资源之间的依赖关系,避免资源重复打包的问题还是要自己解决。Unity5.x提供了一个很好的东西,就是可以给资源设置AssetBundleName

 
 

Unity5.x中会将设置AssetBundleName相同的资源打包到一起,所以我们的打包过程其实就是把需要打包到一起的资源设置成相同的AssetBundleName,为每个资源设置AssetBundleName。最后调用打包API即可完成打包

我目前使用的打包规则是:

  • prefab、scene等文件单独打包
  • 图片资源按文件夹打包

关键代码如下:

  1. /// <summary>
  2. /// 设置AssetBundleName
  3. /// </summary>
  4. /// <param name="fullpath">Fullpath.</param>
  5. public static void setAssetBundleName(string fullPath)
  6. {
  7. string[] files = System.IO.Directory.GetFiles (fullPath);
  8. if (files == null || files.Length == 0) {
  9. return;
  10. }
  11. Debug.Log ("Set AssetBundleName Start......");
  12. string dirBundleName = fullPath.Substring (RES_SRC_PATH.Length);
  13. dirBundleName = dirBundleName.Replace ("/", "@") + ASSET_BUNDLE_SUFFIX;
  14. foreach (string file in files) {
  15. if (file.EndsWith (".meta")) {
  16. continue;
  17. }
  18. AssetImporter importer = AssetImporter.GetAtPath (file);
  19. if (importer != null) {
  20. string ext = System.IO.Path.GetExtension (file);
  21. string bundleName = dirBundleName;
  22. if (null != ext && (ext.Equals (".prefab")||ext.Equals(".unity"))) {
  23. // prefab单个文件打包
  24. bundleName = file.Substring (RES_SRC_PATH.Length);
  25. bundleName = bundleName.Replace ("/", "@");
  26. if (null != ext) {
  27. bundleName = bundleName.Replace (ext, ASSET_BUNDLE_SUFFIX);
  28. } else {
  29. bundleName += ASSET_BUNDLE_SUFFIX;
  30. }
  31. }
  32. bundleName = bundleName.ToLower ();
  33. Debug.LogFormat ("Set AssetName Succ, File:{0}, AssetName:{1}", file, bundleName);
  34. importer.assetBundleName = bundleName;
  35. EditorUtility.UnloadUnusedAssetsImmediate();
  36. // 存储bundleInfo
  37. AssetBuildBundleInfo info = new AssetBuildBundleInfo();
  38. info.assetName = file;
  39. info.fileName = file;
  40. info.bundleName = bundleName;
  41. if (null != ext) {
  42. info.fileName = file.Substring (0, file.IndexOf (ext));
  43. }
  44. fileMap.Add (file, info);
  45. List<AssetBuildBundleInfo> infoList = null;
  46. bundleMap.TryGetValue(info.bundleName, out infoList);
  47. if (null == infoList) {
  48. infoList = new List<AssetBuildBundleInfo> ();
  49. bundleMap.Add (info.bundleName, infoList);
  50. }
  51. infoList.Add (info);
  52. } else {
  53. Debug.LogFormat ("Set AssetName Fail, File:{0}, Msg:Importer is null", file);
  54. }
  55. }
  56. Debug.Log ("Set AssetBundleName End......");
  57. }

打包好之后生成依赖关系的配置文件,配置文件格式如下:

  1. <files>
  2. <file>
  3. <bundleName>prefabs@login.unity3d</bundleName>
  4. <fileName>Assets/Resources/Prefabs/Login</fileName>
  5. <assetName>Assets/Resources/Prefabs/Login.prefab</assetName>
  6. <deps>
  7. <dep>textures@common.unity3d</dep>
  8. <dep>textures@login.unity3d</dep>
  9. </deps>
  10. </file>
  11. <file>
  12. <bundleName>scenes@main.unity3d</bundleName>
  13. <fileName>Assets/Resources/Scenes/Main</fileName>
  14. <assetName>Assets/Resources/Scenes/Main.unity</assetName>
  15. </file>
  16. <file>
  17. <bundleName>textures@common.unity3d</bundleName>
  18. <fileName>Assets/Resources/Textures/Common/btn_blue</fileName>
  19. <assetName>Assets/Resources/Textures/Common/btn_blue.png</assetName>
  20. </file>
  21. <file>
  22. <bundleName>textures@common.unity3d</bundleName>
  23. <fileName>Assets/Resources/Textures/Common/btn_red</fileName>
  24. <assetName>Assets/Resources/Textures/Common/btn_red.png</assetName>
  25. </file>
  26. <file>
  27. <bundleName>textures@login.unity3d</bundleName>
  28. <fileName>Assets/Resources/Textures/Login/bg</fileName>
  29. <assetName>Assets/Resources/Textures/Login/bg.png</assetName>
  30. </file>
  31. <file>
  32. <bundleName>textures@login.unity3d</bundleName>
  33. <fileName>Assets/Resources/Textures/Login/text_input</fileName>
  34. <assetName>Assets/Resources/Textures/Login/text_input.png</assetName>
  35. </file>
  36. </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. apply、call

    call(),apply() 1.每个函数都包含两个非继承而来的方法:call()和apply() 2.在特定的作用域内调用函数,等于设置函数体内的this对象,以扩充函数赖以运行的作用域 3.app ...

  2. Creating a Hadoop-2.x project in Eclipse

    Creating a Hadoop-2.x project in Eclipse hortonworks:MapReduce Ports http://docs.hortonworks.com/HDP ...

  3. 廖雪峰网站:学习python基础知识(一)

    1. python能做什么? 可以做日常任务,比如自动备份你的MP3:可以做网站,很多著名的网站包括YouTube就是Python写的:可以做网络游戏的后台,很多在线游戏的后台都是Python开发的. ...

  4. leetcode-algorithms-22 Generate Parentheses

    leetcode-algorithms-22 Generate Parentheses Given n pairs of parentheses, write a function to genera ...

  5. leetcode-algorithms-3 Longest Substring Without Repeating Characters

    leetcode-algorithms-3 Longest Substring Without Repeating Characters Given a string, find the length ...

  6. Spring + Mybatis项目实现数据库读写分离

    主要思路:通过实现AbstractRoutingDataSource类来动态管理数据源,利用面向切面思维,每一次进入service方法前,选择数据源. 1.首先pom.xml中添加aspect依赖 & ...

  7. 安卓——Handler延迟跳转

    //声明控制对象 Handler handler =new Handler(){ @Override public void handleMessage(Message msg) { super.ha ...

  8. docker实战系列之docker 端口映射错误解决方法

    错误: Error response from daemon: Cannot start container web: iptables failed: iptables -t nat -A DOCK ...

  9. Python3+ssl实现加密通信

    一.说明 1. python标准库ssl可实现加密通信 2. ssl库底层使用openssl,做了面向对像化改造和简化,但还是可以明显看出openssl的痕迹 3. 本文先给出python实现的soc ...

  10. WordDenified.exportedUI

    <mso:cmd app="Word" dt="1" /><mso:customUI xmlns:x1="xuzaAzWord&qu ...