目录
打包与资源加载框架目录
前言
先捋一下打AB包的整体思路,首先,Unity4.6版本之后就使用了全新的打包接口
BuildPipeline.BuildAssetBundles(string outputPath, AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform);
无论是全新打包还是增量打包都是使用这个API,所以一切的一切,都要围绕这个API开始讲起。
该API有四个参数
- string outputPath
- AssetBundleBuild[] builds
- BuildAssetBundleOptions assetBundleOptions
- BuildTarget targetPlatform
参数1:outputPath比较好理解,打包输出路径
参数2:builds的意思是,你要告诉API,哪些资源需要打成ab包,该参数是打AB包的核心所在,资源规划、打包策略都是在生成该参数的时候完成的
参数3:该参数的作用是告诉API一些规则,下面列出了参数3的源代码
public enum BuildAssetBundleOptions
{
None = 0,
UncompressedAssetBundle = 1,
CollectDependencies = 2,
CompleteAssets = 4,
DisableWriteTypeTree = 8,
DeterministicAssetBundle = 16,
ForceRebuildAssetBundle = 32,
IgnoreTypeTreeChanges = 64,
AppendHashToAssetBundleName = 128,
ChunkBasedCompression = 256,
StrictMode = 512,
DryRunBuild = 1024,
DisableLoadAssetByFileName = 4096,
DisableLoadAssetByFileNameWithExtension = 8192,
EnableProtection = 16384
}
参数3的使用方式也比较简单,根据需要选择
private BuildAssetBundleOptions MakeBuildOptions()
{
// For the new build system, unity always need BuildAssetBundleOptions.CollectDependencies and BuildAssetBundleOptions.DeterministicAssetBundle
// 除非设置ForceRebuildAssetBundle标记,否则会进行增量打包
BuildAssetBundleOptions opt = BuildAssetBundleOptions.None;
opt |= BuildAssetBundleOptions.StrictMode; //Do not allow the build to succeed if any errors are reporting during it.
opt |= BuildAssetBundleOptions.DeterministicAssetBundle;
if (CompressOption == ECompressOption.Uncompressed)
opt |= BuildAssetBundleOptions.UncompressedAssetBundle;
else if (CompressOption == ECompressOption.ChunkBasedCompressionLZ4)
opt |= BuildAssetBundleOptions.ChunkBasedCompression;
if (IsForceRebuild)
opt |= BuildAssetBundleOptions.ForceRebuildAssetBundle; //Force rebuild the asset bundles
if (IsAppendHash)
opt |= BuildAssetBundleOptions.AppendHashToAssetBundleName; //Append the hash to the assetBundle name
if (IsDisableWriteTypeTree)
opt |= BuildAssetBundleOptions.DisableWriteTypeTree; //Do not include type information within the asset bundle (don't write type tree).
if (IsIgnoreTypeTreeChanges)
opt |= BuildAssetBundleOptions.IgnoreTypeTreeChanges; //Ignore the type tree changes when doing the incremental build check.
if (UseFullPath)
opt |= (BuildAssetBundleOptions.DisableLoadAssetByFileName | BuildAssetBundleOptions.DisableLoadAssetByFileNameWithExtension);
return opt;
}
看不懂 |=符号的小伙伴需要补一下C#的知识。
参数4:目标平台,这个很简单,需要打包哪个平台就传入哪个枚举即可。
打包流程:
-
检测输出文件夹是否存在,如果不存在则创建Directory.CreateDirectory
-
填写打包目标配置,详见该系列第二篇文章
- 根据打包目标配置,收集所有资源列表,并在该步剔除无效资源。无效资源是需要策略的,或者说是需要配置的,如何配置后面讲
- 设置资源的AssetBundleLabel,这是生成AB包的重要步骤,下图是引擎内设置AssetBundleLabel的地方
-
利用合法资源列表生成AssetBundleManifest.BuildAssetBundles接口能识别的AssetBundleBuild[]数据列表
-
调用打包接口前,要提前创建打包接口的参数3和参数4的变量,参考上文
-
调用AssetBundleManifest.BuildAssetBundles API完成第一次打包,生成的文件如下图标红部分(InitManifest、patchManifest等文件是第二次打包生成的文件)
7、第一次打包API可以返回UnityManifest文件的引用,UnityManifest引用配合第二步的资源列表数据,创建一个UnityAsset文件,这个文件记录了Bundle之间的引用关系。文章来源:https://www.toymoban.com/news/detail-498138.html
第八步:把第七步的UnityAsset文件再次执行打包,生成bundle文件,等待加载。文章来源地址https://www.toymoban.com/news/detail-498138.html
到了这里,关于[游戏开发][Unity]Assetbundle打包篇(1)打包流程介绍的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!