Android 12 Developer Preview is here! Try it out, and give us your feedback!

Integrate asset delivery (Unity)

Use the steps in this guide to access your app’s asset packs from your Unity C# code.

Build for Unity

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

  1. Get the Unity plugin (or package) for Play Asset Delivery.

  2. Create AssetBundles in Unity.

Configure AssetBundles using the UI

  1. Configure each AssetBundle in an asset pack:

    1. Select Google > Android App Bundle > Asset Delivery Settings.
    2. To select folders that directly contain AssetBundle files, click Add Folder.

  2. For each bundle, change the Delivery Mode to Install Time, Fast Follow, or On Demand. Resolve any errors or dependencies and close the window.

  3. Select Google > Build Android App Bundle to build the app bundle.

  4. (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 do not 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.

You implement the API according to the delivery type of the asset pack you wish to access. These steps are shown in the following flowchart.

Asset pack flow diagram for the plugin

Figure 1. Flow diagram for accessing asset packs

Retrieve AssetBundles

Import the Play Asset Delivery library and call the RetrieveAssetBundleAsync() method to retrieve an AssetBundle.

using Google.Play.AssetDelivery;

// Loads the AssetBundle from disk, downloading the asset pack containing it if necessary.
PlayAssetBundleRequest bundleRequest = PlayAssetDelivery.RetrieveAssetBundleAsync(asset-bundle-name);

Install-time delivery

Asset packs configured as install-time are immediately available at app launch. You can use the following to load a scene from the AssetBundle:

AssetBundle assetBundle = bundleRequest.AssetBundle;

// You may choose to load scenes from the AssetBundle. For example:
string[] scenePaths = assetBundle.GetAllScenePaths();
SceneManager.LoadScene(scenePaths[path-index]);

Fast-follow and on-demand delivery

These sections apply to fast-follow and on-demand asset packs.

Check status

Each asset pack is stored in a separate folder in the app's internal storage. Use the isDownloaded() method to determine if an asset pack has already been downloaded.

Monitor the download

Query the PlayAssetBundleRequest object to monitor the status of the request:

// Download progress of request, between 0.0f and 1.0f. The value will always be
// 1.0 for assets delivered as install-time.
// NOTE: A value of 1.0 will only signify the download is complete. It will still need to be loaded.
float progress = bundleRequest.DownloadProgress;

// Returns true if:
//   * it had either completed the download, installing, and loading of the AssetBundle,
//   * OR if it has encountered an error.
bool done = bundleRequest.IsDone;

// Returns status of retrieval request.
AssetDeliveryStatus status = bundleRequest.Status;
switch(status) {
    case AssetDeliveryStatus.Pending:
        // Asset pack download is pending - N/A for install-time assets.
    case AssetDeliveryStatus.Retrieving:
        // Asset pack is being downloaded and transferred to app storage.
        // N/A for install-time assets.
    case AssetDeliveryStatus.Available:
        // Asset pack is downloaded on disk but NOT loaded into memory.
        // For PlayAssetPackRequest(), this indicates that the request is complete.
    case AssetDeliveryStatus.Loading:
        // Asset pack is being loaded.
    case AssetDeliveryStatus.Loaded:
        // Asset pack has finished loading, assets can now be loaded.
        // For PlayAssetBundleRequest(), this indicates that the request is complete.
    case AssetDeliveryStatus.Failed:
        // Asset pack retrieval has failed.
    case AssetDeliveryStatus.WaitingForWifi:
        // Asset pack retrieval paused until either the device connects via Wi-Fi,
        // or the user accepts the PlayAssetDelivery.ShowCellularDataConfirmation dialog.
    default:
        break;
}

Large downloads

Asset packs larger than 150MB can download automatically, but only on Wi-Fi. If the user is not on Wi-Fi, the PlayAssetBundleRequest status is set to AssetDeliveryStatus.WaitingForWifi and the download is paused. In this case, either wait until the device connects to Wi-Fi, resuming the download, or prompt the user for approval to download the pack over a cellular connection.

if(bundleRequest.Status == AssetDeliveryStatus.WaitingForWifi) {
    var userConfirmationOperation = PlayAssetDelivery.ShowCellularDataConfirmation();
    yield return userConfirmationOperation;

    switch(userConfirmationOperation.GetResult()) {
        case ConfirmationDialogResult.Unknown:
            // userConfirmationOperation finished with an error. Something went
            // wrong when displaying the prompt to the user, and they weren't
            // able to interact with the dialog. In this case, we recommend
            // developers wait for Wi-Fi before attempting to download again.
            // You can get more info by calling GetError() on the operation.
        case ConfirmationDialogResult.Accepted:
            // User accepted the confirmation dialog - download will start
            // automatically (no action needed).
        case ConfirmationDialogResult.Declined:
            // User canceled or declined the dialog. Await Wi-Fi connection, or
            // re-prompt the user.
        default:
            break;
    }
}

Cancel a request (on-demand only)

If you need to cancel the request before the AssetBundles are loaded into memory, call the AttemptCancel() method on the PlayAssetBundleRequest object:

// Will only attempt if the status is Pending, Retrieving, or Available - otherwise
// it will be a no-op.
bundleRequest.AttemptCancel();

// Check to see if the request was successful by checking if the error code is Canceled.
if(bundleRequest.Error == AssetDeliveryErrorCode.Canceled) {
    // Request was successfully canceled.
}

Request asset packs asynchronously

In most cases, you should use Coroutines to request asset packs asynchronously and monitor progress, as shown by the following:

private IEnumerator LoadAssetBundleCoroutine(string assetBundleName) {

    PlayAssetBundleRequest bundleRequest =
        PlayAssetDelivery.RetrieveAssetBundleAsync(assetBundleName);

    while (!bundleRequest.IsDone) {
        if(bundleRequest.Status == AssetDeliveryStatus.WaitingForWifi) {
            var userConfirmationOperation = PlayAssetDelivery.ShowCellularDataConfirmation();

            // Wait for confirmation dialog action.
            yield return userConfirmationOperation;

            if((userConfirmationOperation.Error != AssetDeliveryErrorCode.NoError) ||
               (userConfirmationOperation.GetResult() != ConfirmationDialogResult.Accepted)) {
                // The user did not accept the confirmation - handle as needed.
            }

            // Wait for Wi-Fi connection OR confirmation dialog acceptance before moving on.
            yield return new WaitUntil(() => bundleRequest.Status != AssetDeliveryStatus.WaitingForWifi);
        }

        // Use bundleRequest.DownloadProgress to track download progress.
        // Use bundleRequest.Status to track the status of request.

        yield return null;
    }

    if (bundleRequest.Error != AssetDeliveryErrorCode.NoError) {
        // There was an error retrieving the bundle. For error codes NetworkError
        // and InsufficientStorage, you may prompt the user to check their
        // connection settings or check their storage space, respectively, then
        // try again.
        yield return null;
    }

    // Request was successful. Retrieve AssetBundle from request.AssetBundle.
    AssetBundle assetBundle = bundleRequest.AssetBundle;

For more information on handling errors, see the list of AssetDeliveryErrorCodes.

Other Play Core API methods

The following are some additional API methods you may want to use in your app.

Check download size

Check the size of an AssetBundle by making an asynchronous call to Google Play and setting a callback method for when the operation completes:

public IEnumerator GetDownloadSize() {
   PlayAsyncOperation<long> getSizeOperation =
   PlayAssetDelivery.GetDownloadSize(assetPackName);

   yield return getSizeOperation;
   if(operation.Error != AssetDeliveryErrorCode.NoError) {
       // Error while retrieving download size.
    } else {
        // Download size is given in bytes.
        long downloadSize = operation.GetResult();
    }
}

Remove AssetBundles

You can remove fast-follow and on-demand AssetBundles that are not currently loaded into memory. Make the following asynchronous call and set a callback method for when it completes:

PlayAsyncOperation<string> removeOperation = PlayAssetDelivery.RemoveAssetPack(assetBundleName);

removeOperation.Completed += (operation) =>
            {
                if(operation.Error != AssetDeliveryErrorCode.NoError) {
                    // Error while attempting to remove AssetBundles.
                } else {
                    // Files were deleted OR files did not exist to begin with.
                }
            };

Next step

Test asset delivery locally and from Google Play.