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
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。