您可以按照本指南将 Play Asset Delivery 内置到您的 Unity 项目中。
概览
Unity AssetBundle 文件包含可在应用运行时由 Unity 引擎加载的序列化资源。这些文件是平台专用的(例如,针对 Android 构建),并且可与资源包结合使用。最常见的情况是,将一个 AssetBundle 文件打包成单个资源包,并且资源包与 AssetBundle 使用相同的名称。如果您希望更灵活地创建资源包,请使用 API 配置资源包。
在运行时,可使用 Play Asset Delivery for Unity 类来检索打包在资源包中的 AssetBundle。
前提条件
使用界面配置 AssetBundle
配置资源包中的每个 AssetBundle:
- 依次选择 Google > Android App Bundle > Asset Delivery Settings。
- 如需选择直接包含 AssetBundle 文件的文件夹,请点击 Add Folder。
针对每个 Bundle,将 Delivery Mode 更改为 Install Time、Fast Follow 或 On Demand。解决所有错误或依赖项,然后关闭窗口。
依次选择 Google > Build Android App Bundle 以构建 App Bundle。
(可选)配置 App Bundle 以支持不同的纹理压缩格式。
使用 API 配置资源包
您可以通过能作为自动化构建系统的一部分运行的编辑器脚本配置 Asset Delivery。
使用 AssetPackConfig
类定义需要包含在 Android App Bundle build 中的资源,以及这些资源的分发模式。这些资源包无需包含 AssetBundle。
public void ConfigureAssetPacks { // Creates an AssetPackConfig with a single asset pack, named // examplePackName, containing all the files in path/to/exampleFolder. var assetPackConfig = new AssetPackConfig(); assetPackConfig.AddAssetsFolder("examplePackName", "path/to/exampleFolder", AssetPackDeliveryMode.OnDemand); // Configures the build system to use the newly created assetPackConfig when // calling Google > Build and Run or Google > Build Android App Bundle. AssetPackConfigSerializer.SaveConfig(assetPackConfig); // Alternatively, use BundleTool.BuildBundle to build an App Bundle from script. BuildBundle(new buildPlayerOptions(), assetPackConfig); }
在给定 BuildPlayerOptions 和 AssetPackConfig
的情况下,您还可以在 Bundletool
类中使用静态 BuildBundle
方法生成具有资源包的 Android App Bundle。
后续步骤
使用 Unity 将 Play Asset Delivery 集成到您的游戏或应用代码中。