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

第一步:创建打包资源

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

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

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

第三步:开始打包

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

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

using UnityEngine;
using UnityEditor;
using System.Diagnostics;
using System.IO; public class ExportAssetBundles : EditorWindow
{ [@MenuItem("AssetBundles/Build AssetBundles")]
static void main()
{
EditorWindow.GetWindow<ExportAssetBundles>(false,"AssetBundles");
} private Vector2 scrollVec2;
private string rootPath = "Assets";
private string _assetBundleName = "";
private string _assetBundlePath = "Assets/StreamingAssets";
private int _assetBundleElementNum = 4;
private string[] _assetBundleElement = new string[4]; void OnGUI()
{
scrollVec2 = EditorGUILayout.BeginScrollView(scrollVec2, GUILayout.Width(position.width), GUILayout.Height(position.height)); EditorGUILayout.BeginHorizontal();
GUILayout.Label("资源包名称:");
_assetBundleName = EditorGUILayout.TextField(_assetBundleName);
if (GUILayout.Button("清空"))
EditorApplication.delayCall += Delete;
EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal();
GUILayout.Label("资源包路径:");
_assetBundlePath = EditorGUILayout.TextField(_assetBundlePath);
if (GUILayout.Button("浏览"))
EditorApplication.delayCall += Save;
EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal();
GUILayout.Label("资源包容量:");
_assetBundleElementNum = EditorGUILayout.IntField(_assetBundleElementNum);
if (GUILayout.Button("增加"))
EditorApplication.delayCall += Add;
EditorGUILayout.EndHorizontal(); if (_assetBundleElementNum > 0)
{
if (_assetBundleElement.Length != _assetBundleElementNum)
{
string[] temp = _assetBundleElement;
_assetBundleElement = new string[_assetBundleElementNum];
for (int i = 0; i < temp.Length; i++)
{
if (i < _assetBundleElement.Length)
_assetBundleElement[i] = temp[i];
}
}
for (int i = 0; i < _assetBundleElementNum; i++)
{
EditorGUILayout.BeginHorizontal();
GUILayout.Label("资源" + (i + 1) + ":");
_assetBundleElement[i] = EditorGUILayout.TextField(_assetBundleElement[i]);
if (GUILayout.Button("浏览"))
Browse(i);
EditorGUILayout.EndHorizontal();
}
}
if (GUILayout.Button("打包"))
{
if (_assetBundleName == "")
{
//打开一个通知栏
this.ShowNotification(new GUIContent("资源包名称不可为空"));
return;
}
if (_assetBundlePath == "C:/" || _assetBundlePath == "D:/" || _assetBundlePath == "E:/" || _assetBundlePath == "F:/")
{
//打开一个通知栏
this.ShowNotification(new GUIContent("资源包路径不可为根目录"));
return;
}
if (_assetBundleElementNum <= 0)
{
//打开一个通知栏
this.ShowNotification(new GUIContent("资源包容量必须大于0"));
return;
}
for (int i = 0; i < _assetBundleElement.Length; i++)
{
if (_assetBundleElement[i] == null || _assetBundleElement[i] == "")
{
//打开一个通知栏
this.ShowNotification(new GUIContent("资源"+(i+1)+"路径为空"));
return;
}
}
EditorApplication.delayCall += Build;
} EditorGUILayout.EndScrollView();
}
/// <summary>
/// 清空资源包名称
/// </summary>
void Delete()
{
_assetBundleName = "";
//转移焦点至主窗口
EditorUtility.FocusProjectWindow();
}
/// <summary>
/// 选择资源存储路径
/// </summary>
void Save()
{
string path = EditorUtility.OpenFolderPanel("选择要存储的路径", "", "");
if (path.Length != 0)
{
_assetBundlePath = path;
EditorUtility.FocusProjectWindow();
}
}
/// <summary>
/// 资源包容量增加
/// </summary>
void Add()
{
_assetBundleElementNum += 1;
EditorUtility.FocusProjectWindow();
}
/// <summary>
/// 选择单个打包资源
/// </summary>
/// <param name="i">资源序号</param>
void Browse(int i)
{
string path = EditorUtility.OpenFilePanel("选择要打包的资源", @"E:\hutao\Unity Project5.2\Course Cloud Platform\Assets", "*");
if (path.Length != 0)
{
if (path.IndexOf(rootPath) >= 0)
{
//如果选中的资源是dll文件,则自动改后缀名为.bytes
if (path.EndsWith(".dll"))
{
string newpath = path.Substring(0, path.LastIndexOf('.')) + ".bytes";
File.Move(path, newpath);
_assetBundleElement[i] = newpath.Substring(newpath.IndexOf(rootPath));
AssetDatabase.Refresh();
}
else
{
_assetBundleElement[i] = path.Substring(path.IndexOf(rootPath));
}
}
}
}
/// <summary>
/// 打包资源
/// </summary>
void Build()
{
//需要打包的资源(可打包成多个)
AssetBundleBuild[] buildMap = new AssetBundleBuild[1]; //资源包的名称
buildMap[0].assetBundleName = _assetBundleName;
//资源包下的资源名称,一个资源包可以包含多个资源,资源由从Assets开始的路径组成且包含自身后缀名
buildMap[0].assetNames = _assetBundleElement; BuildPipeline.BuildAssetBundles(_assetBundlePath, buildMap);
}
}

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

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. 如何处理JS,css与smarty标签的冲突

    smarty的默认标记符是大括号:{}, 假如我们页面上有JS且定义了函数或者对象,或者有CSS定义了样式,就会出现大括号, smary引擎就会把这些大括号当作smarty语法来解释, 很明显,这些C ...

  2. PHP 文件下载 浅析

    无控制类型 avi文件 rar文件 mp4MP3图片等会被直接解析 核心代码 类型 长度 实现函数 优化 原始下载文件的名称 优化后的文件下载名称 总结 文件下载的功能对一个网站而言基本上是必备的了, ...

  3. 理解性能的奥秘——应用程序中慢,SSMS中快(1)——简介

    本文属于<理解性能的奥秘--应用程序中慢,SSMS中快>系列 在工作中发现有不少类似的现象,有幸看到国外大牛写的一篇文章,由于已经完善得不能再添油加醋,所以决定直接翻译,原文出处:http ...

  4. 亲密接触Redis-第二天(Redis Sentinel)

    简介 经过上次轻松搭建了一个Redis的环境并用Java代码调通后,这次我们要来看看Redis的一些坑以及Redis2.8以后带来的一个新的特性即支持高可用特性功能的Sentinel(哨兵). Red ...

  5. Redis 学习笔记3:Jedis 连接虚拟机下的Redis 服务

    Jedis 是 Redis 官方首选的 Java 客户端开发包. 虚拟机的IP地址是192.168.8.88. Jedis代码是放在windows上的,启动虚拟机上的Redis服务之后,用Jedis连 ...

  6. Python optparser库详解

    一直以来对optparser不是特别的理解,今天就狠下心,静下心研究了一下这个库.当然了,不敢说理解的很到位,但是足以应付正常的使用了.废话不多说,开始今天的分享吧. 简介 optparse模块主要用 ...

  7. 【Android 系统开发】使用 Source InSight 阅读 Android 源码

    1. 安装 Source Insight (1) Source Insight 相关资源 安装相关资源 : -- 下载地址 : http://www.sourceinsight.com/down35. ...

  8. 【iOS 开发】Objective - C 面向对象 - 方法 | 成员变量 | 隐藏封装 | KVC | KVO | 初始化 | 多态

    一. Objective-C 方法详解 1. 方法属性 (1) OC 方法传参机制 Object-C 方法传参机制 : OC 中得参数传递都是值传递, 传入参数的是参数的副本; -- 基本类型 (值传 ...

  9. Linux系统编程----僵尸进程

    什么是僵尸进程? 僵尸进程, 指子进程退出后, 父进程还没有回收子进程的资源,这个子进程就处在于僵尸状态. 来看看如何产生? #include <stdio.h> #include < ...

  10. 插件开发之360 DroidPlugin源码分析(二)Hook机制

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52124397 前言:新插件的开发,可以说是为插件开发者带来了福音,虽然还很多坑要填补, ...