5.x的assetbundle与4.x以及之前的版本有些不同,不过本质是一样的,只不过5.x打包assetbundle更为简单和人性化了,总体来说只需要三个步骤:

第一步:创建打包资源

  1. //这里是一个资源包数组,其中每一个资源包又可以包含多个小资源,所以一般情况下一个资源包就足够了
  2. AssetBundleBuild[] _ABbuild = new AssetBundleBuild[1];

第二步:给资源命名以及指定需要打包的资源

  1. //资源包的名称
  2. _ABbuild[0].assetBundleName = "打包assetbundle出来之后的文件名";
  3. //资源包下的资源名称,一个资源包可以包含多个资源,资源由从Assets开始的路径组成且包含自身后缀名
  4. string[] _allassetname = new string[3];
  5. _allassetname[0] = "Assets/1.png";
  6. _allassetname[1] = "Assets/2.prefab";
  7. _allassetname[2] = "Assets/3.FBX";
  8. _ABbuild[0].assetNames = _allassetname;

第三步:开始打包

  1. //打包到路径E:/test/mytest
  2. BuildPipeline.BuildAssetBundles("E:/test/mytest", _ABbuild);

将之封装成扩展编辑器之后,把如下脚本放在Editor文件夹内:

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Diagnostics;
  4. using System.IO;
  5.  
  6. public class ExportAssetBundles : EditorWindow
  7. {
  8.  
  9. [@MenuItem("AssetBundles/Build AssetBundles")]
  10. static void main()
  11. {
  12. EditorWindow.GetWindow<ExportAssetBundles>(false,"AssetBundles");
  13. }
  14.  
  15. private Vector2 scrollVec2;
  16. private string rootPath = "Assets";
  17. private string _assetBundleName = "";
  18. private string _assetBundlePath = "Assets/StreamingAssets";
  19. private int _assetBundleElementNum = 4;
  20. private string[] _assetBundleElement = new string[4];
  21.  
  22. void OnGUI()
  23. {
  24. scrollVec2 = EditorGUILayout.BeginScrollView(scrollVec2, GUILayout.Width(position.width), GUILayout.Height(position.height));
  25.  
  26. EditorGUILayout.BeginHorizontal();
  27. GUILayout.Label("资源包名称:");
  28. _assetBundleName = EditorGUILayout.TextField(_assetBundleName);
  29. if (GUILayout.Button("清空"))
  30. EditorApplication.delayCall += Delete;
  31. EditorGUILayout.EndHorizontal();
  32.  
  33. EditorGUILayout.BeginHorizontal();
  34. GUILayout.Label("资源包路径:");
  35. _assetBundlePath = EditorGUILayout.TextField(_assetBundlePath);
  36. if (GUILayout.Button("浏览"))
  37. EditorApplication.delayCall += Save;
  38. EditorGUILayout.EndHorizontal();
  39.  
  40. EditorGUILayout.BeginHorizontal();
  41. GUILayout.Label("资源包容量:");
  42. _assetBundleElementNum = EditorGUILayout.IntField(_assetBundleElementNum);
  43. if (GUILayout.Button("增加"))
  44. EditorApplication.delayCall += Add;
  45. EditorGUILayout.EndHorizontal();
  46.  
  47. if (_assetBundleElementNum > 0)
  48. {
  49. if (_assetBundleElement.Length != _assetBundleElementNum)
  50. {
  51. string[] temp = _assetBundleElement;
  52. _assetBundleElement = new string[_assetBundleElementNum];
  53. for (int i = 0; i < temp.Length; i++)
  54. {
  55. if (i < _assetBundleElement.Length)
  56. _assetBundleElement[i] = temp[i];
  57. }
  58. }
  59. for (int i = 0; i < _assetBundleElementNum; i++)
  60. {
  61. EditorGUILayout.BeginHorizontal();
  62. GUILayout.Label("资源" + (i + 1) + ":");
  63. _assetBundleElement[i] = EditorGUILayout.TextField(_assetBundleElement[i]);
  64. if (GUILayout.Button("浏览"))
  65. Browse(i);
  66. EditorGUILayout.EndHorizontal();
  67. }
  68. }
  69. if (GUILayout.Button("打包"))
  70. {
  71. if (_assetBundleName == "")
  72. {
  73. //打开一个通知栏
  74. this.ShowNotification(new GUIContent("资源包名称不可为空"));
  75. return;
  76. }
  77. if (_assetBundlePath == "C:/" || _assetBundlePath == "D:/" || _assetBundlePath == "E:/" || _assetBundlePath == "F:/")
  78. {
  79. //打开一个通知栏
  80. this.ShowNotification(new GUIContent("资源包路径不可为根目录"));
  81. return;
  82. }
  83. if (_assetBundleElementNum <= 0)
  84. {
  85. //打开一个通知栏
  86. this.ShowNotification(new GUIContent("资源包容量必须大于0"));
  87. return;
  88. }
  89. for (int i = 0; i < _assetBundleElement.Length; i++)
  90. {
  91. if (_assetBundleElement[i] == null || _assetBundleElement[i] == "")
  92. {
  93. //打开一个通知栏
  94. this.ShowNotification(new GUIContent("资源"+(i+1)+"路径为空"));
  95. return;
  96. }
  97. }
  98. EditorApplication.delayCall += Build;
  99. }
  100.  
  101. EditorGUILayout.EndScrollView();
  102. }
  103. /// <summary>
  104. /// 清空资源包名称
  105. /// </summary>
  106. void Delete()
  107. {
  108. _assetBundleName = "";
  109. //转移焦点至主窗口
  110. EditorUtility.FocusProjectWindow();
  111. }
  112. /// <summary>
  113. /// 选择资源存储路径
  114. /// </summary>
  115. void Save()
  116. {
  117. string path = EditorUtility.OpenFolderPanel("选择要存储的路径", "", "");
  118. if (path.Length != 0)
  119. {
  120. _assetBundlePath = path;
  121. EditorUtility.FocusProjectWindow();
  122. }
  123. }
  124. /// <summary>
  125. /// 资源包容量增加
  126. /// </summary>
  127. void Add()
  128. {
  129. _assetBundleElementNum += 1;
  130. EditorUtility.FocusProjectWindow();
  131. }
  132. /// <summary>
  133. /// 选择单个打包资源
  134. /// </summary>
  135. /// <param name="i">资源序号</param>
  136. void Browse(int i)
  137. {
  138. string path = EditorUtility.OpenFilePanel("选择要打包的资源", @"E:\hutao\Unity Project5.2\Course Cloud Platform\Assets", "*");
  139. if (path.Length != 0)
  140. {
  141. if (path.IndexOf(rootPath) >= 0)
  142. {
  143. //如果选中的资源是dll文件,则自动改后缀名为.bytes
  144. if (path.EndsWith(".dll"))
  145. {
  146. string newpath = path.Substring(0, path.LastIndexOf('.')) + ".bytes";
  147. File.Move(path, newpath);
  148. _assetBundleElement[i] = newpath.Substring(newpath.IndexOf(rootPath));
  149. AssetDatabase.Refresh();
  150. }
  151. else
  152. {
  153. _assetBundleElement[i] = path.Substring(path.IndexOf(rootPath));
  154. }
  155. }
  156. }
  157. }
  158. /// <summary>
  159. /// 打包资源
  160. /// </summary>
  161. void Build()
  162. {
  163. //需要打包的资源(可打包成多个)
  164. AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
  165.  
  166. //资源包的名称
  167. buildMap[0].assetBundleName = _assetBundleName;
  168. //资源包下的资源名称,一个资源包可以包含多个资源,资源由从Assets开始的路径组成且包含自身后缀名
  169. buildMap[0].assetNames = _assetBundleElement;
  170.  
  171. BuildPipeline.BuildAssetBundles(_assetBundlePath, buildMap);
  172. }
  173. }

在编辑器中效果图如下:(其中每个资源小项可以是工程中的文件夹,如果是文件夹的话那么该文件夹内所有资源都会打包进去)

Unity 5.X扩展编辑器之打包assetbundle的更多相关文章

  1. 实力封装:Unity打包AssetBundle(一)

    说明:这是一系列循序渐进的教程,今天先介绍最简单的AssetBundle打包方式. 这是一个由在Unity中需要加载模型而引发出来的一系列坑,为了填坑花了不少时间,如果有需要在Unity中自定义菜单, ...

  2. 实力封装:Unity打包AssetBundle(大结局)

    →→前情提要:让用户选择要打包的文件←← 大结局:更多选择 Unity打包AssetBundle从入门到放弃系列终于要迎来大结局了[小哥哥表示实在写不动了o(╥﹏╥)o]... 经过上一次的教程,其实 ...

  3. lua------------------Unity3D研究院编辑器之打开unity不可识别的文件(十三)

    Unity3D研究院编辑器之打开unity不可识别的文件(十三) 雨松MOMO [Unity3D拓展编辑器] 围观8597次 9 条评论 编辑日期:2017-03-02 字体:大 中 小   有些特殊 ...

  4. Unity学习(六)5.x依赖打包

    http://blog.sina.com.cn/s/blog_89d90b7c0102w2ox.html unity5已经封装好了接口,所以依赖打包并没有那么神秘和复杂了. 打包: 1.定义好资源的a ...

  5. Emacs和Vim:神的编辑器和编辑器之神, 到底哪个更好?

    Emacs和Vim:神的编辑器和编辑器之神, 到底哪个更好? 在这个蔚蓝色的星球上,流传着两大神器的传说:据说Emacs是神的编辑器,而Vim是编辑器之神. 一些人勇敢地拾起了Vim或Emacs,却发 ...

  6. Win10系统下安装编辑器之神(The God of Editor)Vim并且构建Python生态开发环境(2020年最新攻略)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_160 众神殿内,依次坐着Editplus.Atom.Sublime.Vscode.JetBrains家族.Comodo等等一众编辑 ...

  7. 编辑器之神VIM 总结(一) 基础部分

     版本号 说明 作者 日期  1.0  vim基础知识 Sky Wang 2013/06/19       概要 vim和emacs,一个是编辑器之神,一个是神一样的编辑器.他们被称是UNIX系统下的 ...

  8. 优测优社区干货精选|老司机乱谈编辑器之神——vim

    文 / 腾讯 吴双 前言 优测小优 有话说: 腾讯优测只有应用测试大神?不不不,我们还有各种研发大牛! *** vim 是一种信仰,我自从2004年有了这个信仰,已经12个年头了.本文介绍了学习vim ...

  9. 自由软件之父、Google+设计者、Java之父、Linux之父、万维网之父、Vi编辑器之父、苹果Lisa电脑界面设计、微软首席软件架构师

    自由软件之父.Google+设计者.Java之父.Linux之父.万维网之父.Vi编辑器之父.苹果Lisa电脑界面设计.微软首席软件架构师 理查德·斯托曼(Richard Stallman) 理查德· ...

随机推荐

  1. CentOS环境下使用GIT基于Nginx的私服搭建全过程

    阅读本文前你必须预先装好CentOS并且已经安装和配置好Nginx了. 安装GIT私服套件 安装centos6.5-centos7.0 安装nginx yum install -y?git gitwe ...

  2. J2EE中MVC的各层的设计原则及其编写注意事项

    总结了下J2EE的MVC模式开发原则,很多细节处理好了是很有利于开发与维护的. 下面就从各层说起. 视图层 主要是客户端的显示,主要是JSP和HTML,随着Web的不断发展,许多基于Javascrip ...

  3. springMVC源码分析--AbstractHandlerMapping(二)

    上一篇博客springMVC源码分析--HandlerMapping(一)中我们简单的介绍了HandlerMapping,接下来我们介绍一下它的抽象实现类AbstractHandlerMapping

  4. ROS机器人程序设计(原书第2版)补充资料 (柒) 第七章 3D建模与仿真 urdf Gazebo V-Rep Webots Morse

    ROS机器人程序设计(原书第2版)补充资料 (柒) 第七章 3D建模与仿真 urdf Gazebo V-Rep Webots Morse 书中,大部分出现hydro的地方,直接替换为indigo或ja ...

  5. Java中synchronized的使用实例

    一.使用场景 在负责后台开发的时候,很多时候都是提供接口给前端开发人员去调用,会遇到这样的场景: 需要提供一个领奖接口,每个用户名只能领取一次,我们可以将成功领取的用户在数据库用个标记保存起来.如果这 ...

  6. 深入浅出如何解析xml文件---下篇

    在上篇博文中,小编主要介绍xml的两种解析方式,分别是dom4j和dom,今天这篇博文,小编主要来简单介绍一下xml的其她两种解析方式sax和jdom.  sax解析xml文件 sax,全称是Simp ...

  7. LMAX高并发系统架构

    很早就看到过MF的这篇The LMAX Architecture,可是之前一来英文水平不够,二来确实看不懂- 今天有幸再次看到,一口气读完终于有所领悟. 1 Overall Architecture ...

  8. 验证码程序Demo

    小伙伴都有这样的经历,册各种网站,总是输不对验证码,双十一那天狂买的小伙伴是不是对输入验证码有着不一样的感触呢,以前觉得验证码真是个麻烦鬼,一个不小心,一个眼拙,哎呦,没有输入正确,又是一阵子大眼瞪小 ...

  9. Zookeeper总概

    zookeeper是一个开源的分布式协调服务.是典型的分布式数据一致性的解决方案. zookeeper可以保证以下分布式一致性的特性 1. 顺序性:同一客户端发起的事务请求,最终会严格的按照发出顺序应 ...

  10. Android开发学习之路--React-Native之初体验

      近段时间业余在学node.js,租了个阿里云准备搭建后端,想用node.js,偶尔得知react-native可以在不同平台跑,js在iOS和android上都可以运行ok,今天就简单学习下rea ...