When integrating asset delivery, Unity games can access asset packs using Addressables or AssetBundles. Addressables are the more recent and recommended asset delivery solution for games built with Unity 2019.4 or higher, while AssetBundles provide support of asset packs in Unity 2017.4 and 2018.4.
Unity Addressables
Games built with Unity 2019.4 or higher should use Addressables for asset delivery on Android. Unity provides a Play Asset Delivery (PAD) API for handling Android asset packs using Addressables. For information about using Addressables, see the following:
- Addressables for Android package
- PAD guide for Unity
- PAD API for Unity reference documentation
Use AssetBundle files
Games built with Unity 2017.4 and 2018.4 can use AssetBundle files for asset delivery on Android. Unity AssetBundle files contain serialized assets that can be loaded by the Unity engine while the app is running. These files are platform-specific (for example, built for Android) and can be used in combination with asset packs. Most commonly, one AssetBundle file is packaged into a single asset pack, with the pack using the same name as the AssetBundle. If you want more flexibility in creating an asset pack, configure the asset pack using the API.
At runtime, use the Play Asset Delivery for Unity class to retrieve an AssetBundle packaged in an asset pack.
Prerequisites
- Set up your development environment:
OpenUPM-CLI
If you have the OpenUPM CLI installed you can install the OpenUPM registry with the following command:
openupm add com.google.play.assetdeliveryOpenUPM
Open the package manager settings by selecting the Unity menu option Edit > Project Settings > Package Manager.
Add OpenUPM as a scoped registry to the Package Manager window:
Name: package.openupm.com URL: https://package.openupm.com Scopes: com.google.external-dependency-manager com.google.play.common com.google.play.core com.google.play.assetdelivery com.google.android.appbundleOpen the package manager menu by selecting the Unity menu option Window > Package Manager.
Set the manager scope drop-down to select My Registries.
Select the Google Play Integrity plugin for Unity package from the package list and press Install.
Import from GitHub
Download the latest
.unitypackagerelease from GitHub.Import the
.unitypackagefile by selecting the Unity menu option Assets > Import package > Custom Package and importing all items.
Configure AssetBundles using the UI
Configure each AssetBundle in an asset pack:
- Select Google > Android App Bundle > Asset Delivery Settings.
- To select folders that directly contain AssetBundle files, click Add Folder.
For each bundle, change the Delivery Mode to Install Time, Fast Follow, or On Demand. Resolve any errors or dependencies and close the window.
Select Google > Build Android App Bundle to build the app bundle.
(Optional) Configure your app bundle to support different texture compression formats.
Configure asset packs using the API
You can configure asset delivery through editor scripts which can be run as part of an automated build system.
Use the
AssetPackConfig
class to define which assets to include in an Android App Bundle build, as well
as the delivery mode of the assets. These asset packs don't need to contain
an 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);
}
You can also use the static
BuildBundle
method in the Bundletool class to generate an Android App Bundle with asset
packs, given
BuildPlayerOptions
and
AssetPackConfig.
For a guided tutorial, see the Using Play Asset Delivery in Unity games Codelab.
Integrate with Play Asset Delivery Unity API
The Play Asset Delivery Unity API provides the functionality for requesting asset packs, managing downloads, and accessing the assets. Make sure to Add the Unity plugin into your project first.
The functions you use in the API depend on how you created the asset packs.
If you created asset packs using the plugin UI, select Plugin-configured asset packs.
If you created asset packs using the API (or plugin UI), select API-configured asset packs.
Next steps
Test asset delivery locally and from Google Play.