AssetBundle打包详解
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
![](http://upload-images.jianshu.io/upload_images/3791763-e91a56e1f8de9d78.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
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
包里的AssetNamedeps
依赖的其他bundleName
上述过程就完成了打包,具体可以参考的github: https://github.com/aodota/TestUnity
作者:Aodota
链接:https://www.jianshu.com/p/4648a400b721
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
AssetBundle打包详解的更多相关文章
- VS2010开发程序打包详解
VS2010开发程序打包详解 转自:http://blog.sina.com.cn/s/blog_473b385101019ufr.html 首先打开已经完成的工程,如图: 下面开始制作安装程序包. ...
- Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例)
Hadoop基础-Idea打包详解之手动添加依赖(SequenceFile的压缩编解码器案例) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.编辑配置文件(pml.xml)(我 ...
- python 打包详解
基本步骤: 1. 写setup.py 2. 运行“python setup.py sdist” 3. 在当前目录下会生成文件夹“dist”,打包好的代码就在dist中,以“.tar.gz”的形式被压缩 ...
- UDK游戏打包详解
转自:http://blog.sina.com.cn/s/blog_944177030100ycki.html 安装完的udk目录下有4个主要的文件夹 Binaries -这个文件夹包含游戏的exe程 ...
- [Android Pro] Java进阶学习:jar打包详解
jar文件听说过吗,没有?或者陌生!好,没关系,这就是我们的第一站:打包发布. 为什么会有这个玩意呢,首先,这是jar的全称:JavaTM Archive (JAR) file,是的,就是java存档 ...
- 使用intellJ导入非maven,gradle等非构建工程的依赖,发布工程时候的打包详解
一.导入 1.java项目在没有导入该jar包之前,如图: 2.点击 File -> Project Structure(快捷键 Ctrl + Alt + Shift + s),点击Proje ...
- web程序打包详解
重要更新:鉴于很多小伙伴们说看不到图,我这边换了几个浏览器看了下,都看得到的,估计是网速问题,请耐心等待,另外,为了更好的方便大家学习,特此提供源码以及一个word文档,word文档就是本文内容 ...
- Eclipse jar打包详解
通过Eclipse下的演示工程,介绍如何打包这样的项目:要导出的类里边用到了别的jar包. 方法/步骤 1. Eclipse下的演示工程结构如下图所示,其中Task.java是当前工程运行的M ...
- Android系列之Android 命令行手动编译打包详解
Android 命令行手动编译打包过程图 [详细步骤]: 1使用aapt生成R.java类文件: 例: E:\androidDev\android-sdk-windows2.2\tools> ...
随机推荐
- 部署--云服务器(RubyChina上的转帖); 附加用cap部署sidekiq
https://ruby-china.org/topics/36899 附加https://ruby-china.org/topics/36899 Capistrano + Rails5.2部署 使用 ...
- POJ-2955 Brackets(括号匹配问题)
题目链接:http://poj.org/problem?id=2955 这题要求求出一段括号序列的最大括号匹配数量 规则如下: the empty sequence is a regular brac ...
- 宽度优先搜索BFS(Breadth-First-Search)
Breadth-First-Search 1. 与DFS的异同 相同点:搜索所有可能的状态. 不同点:搜索顺序. 2. BFS总是先搜索距离初始状态近的状态,它是按照:开始状态->只需一次转移就 ...
- python3 设置滚动条
#!python3#coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.by import Byimpor ...
- Sereja and Two Sequences CodeForces - 425C (dp)
大意: 给定序列$a,b$, 每次可以任取两个相同大小的$a_i,b_j$删除$a_i,b_j$左侧所有元素, 花费为e, 得分1, 最后结束时必须再花费之前删除元素的个数, 不得分. 初始能量$s$ ...
- git分支重命名
git branch - m old_name new_name
- leetcode-algorithms-5 Longest Palindromic Substring
leetcode-algorithms-5 Longest Palindromic Substring Given a string s, find the longest palindromic s ...
- IQueryable 与 IEnumberable 接口的区别
IQueryable 与 IEnumberable 接口的区别是: IEnumberable<T> 泛型类在调用自己的 SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了, ...
- 将本地项目推送至gitee或者github
将本地项目推送到Git github上的版本和本地版本冲突的解决方法 初始化项目时,在git中新建项目. 在Github中创建了一个Repository之后,会给你列出如何将自己本地项目Push到Gi ...
- git reset --hard 恢复
git reset --hard ,再然后,悲剧上演~ 恢复方法: 使用 git reflog 来找到最近提交的信息,这里贴出部分信息: F:\voidy>git reflog WARNING: ...