Version: 2020.3
对 APK 扩展文件 (OBB) 的支持
为应用程序打补丁以实现快速开发迭代

Play Asset Delivery

Play Asset Delivery (PAD) is a Google Play Store feature you can use to deliver applications that are larger than 150MB. Instead of using APK expansion files (OBB) to store additional assets (such as textures, sounds, and meshes), PAD uses asset packs. Google hosts and serves asset packs on Google Play, which means you don’t need to create a content delivery network to send application resources to users. For more information about PAD, see Android’s Play Asset Delivery.

Important: PAD is only available for the Google Play Store. If you have a large application and want to support other digital distribution services, use APK expansion files (OBB).

Using Play Asset Delivery

To use Play Asset Delivery, you need to set up your project to build Android App Bundles and split the application binary.

To configure Unity to build Android App Bundles:

  1. Open Build Settings (menu: File > Build Settings).
  2. In Platform, select Android.
  3. If Export Project is enabled, enable Export for App Bundle. Otherwise, enable Build App Bundle (Google Play).

To configure Unity to split the application binary:

  1. Open Player Settings (menu: Edit > Project Settings then select Player).
  2. Select the Android settings tab and open the Publishing Settings section.
  3. Enable Split Application Binary.

Now when you build your application, Unity generates an Android App Bundle that includes your application split into a base module and asset packs.

  • Base module: Contains the executables (Java and native), plug-ins, scripts, and assets in the first scene. The first scene is the scene that has a build index of 0.
  • Asset packs: Contains everything else, including the remaining scenes, resources, and streaming assets. For more information about the asset packs that Unity generates at build time, see Generated asset packs.

Alongside the asset packs that Unity generates automatically, you can also create your own custom asset packs. This is useful when you need to control what an asset pack contains. Unity adds your custom asset packs to the final Android App Bundle. For more information about custom asset packs and how to set them up, see Custom asset packs.

Generated asset packs

Asset packs have download size limits. To account for this, Unity changes how it generates asset packs depending on the size of your additional assets:

  • If the additional assets take less than 1GB of storage, Unity packs everything into a single asset pack with the install-time delivery mode. If you do not create any custom asset packs, this means that the device downloads the asset pack as part of the application installation and, when the user first launches the application, all assets are available.
  • If the additional assets take more than 1GB of storage, Unity packs streaming assets into one asset pack and packs all other additional assets into another asset pack. Unity assigns the install-time delivery mode to the larger asset pack and assigns the fast-follow delivery mode to the smaller one.

Note: If either of these asset packs is larger than the upload limit the Google Play Store allows, Unity displays a warning but doesn’t fail the build. Also, Unity checks the sizes of asset packs individually and doesn’t perform size verification for custom asset packs. This means that, if a combination of custom asset packs and asset packs that Unity generates is too large for the Google Play Store, Unity doesn’t display a warning or error.

For asset packs that Unity automatically generates, Unity does not support changing the delivery mode. If you want to change the delivery mode of an asset pack, create custom asset packs with your assets.

Managing asset packs at runtime

Unity provides APIs to manage asset packs at runtime. They use Google’s PlayCore API, which means they have the same limitations as PlayCore, and can’t manage install-time asset packs. Using the PlayCore API also means your application requires the PlayCore plugin. If your project has asset packs, either custom asset packs or Unity-generated asset packs, Unity automatically adds the PlayCore dependency to the application’s manifest.

The way you download asset packs and access their assets depends on the asset pack delivery mode. There are three asset pack delivery modes:

  • install-time: Google Play automatically downloads install-time asset packs when the device installs the application. Google Play considers these asset packs to be part of the base application, and an end user can’t uninstall them without uninstalling the entire application. The PlayCore API doesn’t handle install-time asset packs, which means that you can’t check the status, request to download, or remove install-time asset packs. You also can’t directly access assets inside of these asset packs, except streaming assets in Unity-generated install-time asset packs. To access streaming assets, use Application-streamingAssetsPath to get the path to streaming assets location, then use UnityWebRequest to access assets in that path. If you create a custom asset pack, you can’t access assets inside it using standard file APIs. Instead, use Android’s AssetManager APIs.
  • fast-follow: Google Play automatically starts to download fast-follow asset packs after it installs the application. However, it is possible that not all fast-follow asset packs are available on the first time the application launches. To check the status and download fast-follow asset packs, see below.
  • on-demand: Google Play doesn’t automatically download on-demand asset packs. You have to manually start the download. For information on how to do this, see below.

For more information about delivery modes, see Delivery modes.

If your application uses fast-follow or on-demand asset packs, the device must download these asset packs before the application can access assets inside of them. To check the status of asset packs and download them if they are not on the device, you must first know the name of each asset pack. To get the names of Unity-generated asset packs, call AndroidAssetPacks.GetCoreUnityAssetPackNames. There is no runtime API to get the names of custom asset packs so you must keep track of them yourself. You set the name of custom asset packs at build time; it’s the name of the directory.

After you have the names of your asset packs, to check the status of each asset pack, call AndroidAssetPacks.GetAssetPackStateAsync, passing in the asset pack name. This returns the status of the asset pack you query, and you can use the result to determine whether you need to download the asset pack. If you want to quickly query the status of every Unity-generated asset pack, you can use AndroidAssetPacks.coreUnityAssetPacksDownloaded. This is useful because you must ensure that every Unity-generated asset pack is available before you load any scene other than the first one or try to access other resources that Unity handles.

For every asset pack you need to download, call AndroidAssetPacks.DownloadAssetPackAsync, passing in the asset pack name. While the asset pack downloads, monitor the download status, because downloads can pause or fail. There are two ways to do this:

Custom asset packs

If you want to control which non-code resources are in a particular asset pack, you can create a custom asset pack. Unlike Unity-generated asset packs, you can set the delivery mode for custom asset packs. If you create a custom asset pack, be aware that the Google Play Store has size and quantity limits for asset packs. For information on the limits, see Download size limits.

To create a custom asset pack, create a directory with a name that ends with .androidpack. You can place this directory anywhere in your project’s Assets directory, or any subdirectory. For example, to create a custom asset pack named MyAssets1:

  1. Go to the directory you want to create the asset pack in. This could be directly in Assets or a subdirectory like Assets/CustomAssetPacks.
  2. Create a new directory and call it MyAssets1.androidpack. To add any assets to the asset pack, place them inside this folder. Note: Unity doesn’t include empty asset packs in builds. Also, asset pack names must begin with a letter and consist of English alphanumeric characters or an underscore.
  3. By default, the delivery mode is on-demand, which means that if you don’t change the delivery mode, you need to manually download the asset pack at runtime. For information on how to do this, see Managing asset packs at runtime.
  4. To use a different delivery mode, create a file called build.gradle inside the custom asset pack directory. Paste the following into the file:
apply plugin: 'com.android.asset-pack'
assetPack {
    packName = "MyAssets1"
    dynamicDelivery {
        deliveryType = "fast-follow"
    }
}

This sets the delivery mode to fast-follow, which means Google Play automatically downloads the asset pack after it installs the application. For information on the format of this file, see Integrate asset delivery.

Note: The packName you specify in the build.gradle file should match the asset pack directory name you set without the .androidpack extension.

对 APK 扩展文件 (OBB) 的支持
为应用程序打补丁以实现快速开发迭代