1.方式1-设置AssetBundleName打包
设置AssetBundleName调用打包函数。Unity会将AssetBundle相同名字的资源打包到一个AssetBundle中。
AssetBundle设置Name由两种方式:
1.编辑器编辑,如上图。
2.代码修改(一般都是代码修改,AssetBundleName有斜杆,会斜杆前会创建文件夹,例如chen/logo,会在导出目录下边创建文件夹名chen,再在chen文件夹下创建名为logo的AssetBundle)
static void ChangeAssetbundleName(){Object[] selects = Selection.objects;foreach (Object selected in selects){//返回所有对象相对于工程目录的存储路径如Assets/_Scenes/Main.unitystring path = AssetDatabase.GetAssetPath(selected);//把一个目录的对象检索为AssetImporterAssetImporter asset = AssetImporter.GetAtPath(path);asset.assetBundleName = selected.name; //设置Bundle文件的名称 asset.assetBundleVariant = "unity3d";//设置Bundle文件的扩展名 asset.SaveAndReimport();}AssetDatabase.Refresh();}
调用函数:
// 所有打包操作需要在编辑器模式下,打包代码也需要放在Editor文件夹下// outputPath:输出路径,即工程目录下面的路径// assetBundleOptions:AB包发布选项.// targetPlatform:选择发布平台.// Returns:返回一个包含发布所有信息的AssetBundleManifest对象const string outputPath = "Assets/StreamingAssets";[MenuItem("AssetsBundle/CreateAssetBundle")]static void CreateAssetbundle(){BuildPipeline.BuildAssetBundles(outputPath, 0, EditorUserBuildSettings.activeBuildTarget);AssetDatabase.Refresh();}
调用该函数,unity会自动根据资源的标签进行打包,而且是增量打包,
对于已经打包成AssetBundle的资源,资源没有变动,不会触发重新打包;资源没变,即使资源对应的bundle包被删除了,unity也不会重新打包;生成目录下的bundle包对应的manifase被删了,会重新打包;可以使用BuildAssetBundleOptions.ForceRebuildAssetBundle参数触发强制重新打包。
AssetBundleVariant:
设置AssetBundle的不同变体,比如常用的普清,高清版本。
Unity将对两个AssetBundle中的资源使用同样的ID,使它们支持在运行时切换。
设置变体之前,一定要设置前面的AssetBundle,不能是默认的None,代码上边有。
方式2-通过代码手动设置打包AssetBundle
const string AssetBundlesOutputPath = "Assets/StreamingAssets";[MenuItem("AssetsBundle/CreateAssetBundle")]static void CreateAssetbundle1(){AssetBundleBuild tempAssetBundleBuild = new AssetBundleBuild();tempAssetBundleBuild.assetBundleName = "myAssetBundle"; // abb.assetBundleVariant = "hd";tempAssetBundleBuild.assetNames = new string[2] { @"Assets/SampleAssets/Logo/Logo.prefab", @"Assets/SampleAssets/Logo/UnityLogo/UnityLogo.png" };BuildPipeline.BuildAssetBundles(AssetBundlesOutputPath,new AssetBundleBuild[1] { tempAssetBundleBuild },BuildAssetBundleOptions.None,EditorUserBuildSettings.activeBuildTarget);AssetDatabase.Refresh();}
一个AssetBundleBuild对应一个AssetBundle资源包。如果需要在导出路径下创建文件夹,然后在文件夹下创建AssetBundle,只需修改上边代码中的:tempAssetBundleBuild.assetBundleName = "CustomFolderName/myAssetBundle";用代码手动设置打包AssetBundle,同名的.mainfest中不能自动收集此AssetBundle依赖的AssetBundle。
注意事项:
1.AssetBundle同名的Mainfest
每个AssetBundle都对应一个同名.mainfest,.mainfest包含此AssetBundle中包含的资源。设置AssetBundleName打包生成的同名.mainfest中有此AssetBundle依赖的AssetBundle资源路径。创建AssetBundleBuild对象生成AssetBundle,.mainfest不会自动收集此AssetBundle依赖的AssetBundle信息。由于同名.mainfest只是在Unity Editor打包时候,做增量打包AssetBundle用,所以路径使用的是本地绝对路劲。
Manifest文件(增量打包中使用,运行时不需要,不需要打包到正式包中):
CRC(循环冗余校验码),用于网络下载数据后,数据正确性校验。循环冗余校验
AssetFileHash:AssetBundle中所有asset文件的hash,一个单独的hash。只用来做增量build时的检查。
TypeTreeHash:AssetBundle中所有类型的hash,只用来做增量build时的检查。
ClassTypes:AssetBundle中包含的所有类型(类型对照),这些数据用来得到一个新的单独的hash当typetree做增量build检测。
Assets:包含的所有资源的名称。
Dependencies:此Assetbundle依赖的AssetBundle的名字。
比如修改了预制体名称,重新打包,预制体对应的AssetBundle的CRC变化,AssetFileHash变化,TypeTreeHash不变(没有新加类型)。记录所有AssetBundle依赖关系的.mainfest中的CRC变化。
2.BuildAssetBundleOptions
BuildPipeline.BuildAssetBundles 创建AssetBundle时候,有BuildAssetBundleOptions选项:
3.BuildTarget
选择导出哪个平台使用的AssetBundle。
参考:
/lodypig/article/details/51871510
/sifenkesi/p/6880068.html
/pinkfloyd/p/6489979.html
/530/Documentation/Manual/AssetBundleInternalStructure.html
/Manual/AssetBundles-Building.html