用到AssetBundle技术的项目绝大多数是为了热更新资源,所谓热更新就是在项目有新功能的时候,通过外部资源更新而不用整包更新到设备上,不仅方便后期策划的频繁变动又不会造成用户体验的一种更新方法。AssetBundle可以更新资源(贴图、预制件、音效等等),也可以更新代码(Lua)。这里先学习下更新资源。
1. unity5.x Assetbundle打包
Unity5.xAssetbundle唯一的打包API:
public static AssetBundleManifest BuildAssetBundles(string outputPath, BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.None,BuildTarget targetPlatform = BuildTarget.WebPlayer);
outputPath:生存出来的AB包路径
assetBundleOptions :打包设置(压缩格式)
targetPlatform:打包的目标平台(不同平台有不同的打包格式)调用函数会将所有项目工程下设置过AssetbundleName的资源文件打包:
注意,AssetbundleName后面需要添加后缀。并且所有assetbundlename都为小写,这在加载时候需要注意。
Assetbundle会在加载资源的在打包后生成的文件夹中,每个Bundle都会对应一个manifest文件,记录了Bundle的一些信息,但这类manifest只在增量式打包时才用到;同时,根目录下还会生成一个同名manifest文件及其对应的Bundle文件,通过该Bundle可以在运行时得到一个AssetbundleManifest对象,而所有的Bundle以及各自依赖的Bundle都可以通过该对象提供的接口进行获取。时候才进行解压,只是解压资源的时机不一样。