Provides methods for handling Android asset packs.
Methods in this class are either direct wrappers of java APIs in Google's PlayCore plugin, or depend on values that the PlayCore API returns. Therefore to use it, the gradle project must include the "com.google.android.play:core" dependency. If your project contains custom asset packs or you enable Split Application Binary in Player Settings, Unity automatically adds this dependency to the unityLibrary submodule's build.gradle file. If the PlayCore plugin is missing, calling any wrapper throws an InvalidOperationException exception. Note that PlayCore APIs only work with fast-follow and on-demand delivery type asset packs, therefore methods in this class have the same limitation.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Android;
// Demonstrates a complete asset pack workflow. public class AndroidAssetPacksExample : MonoBehaviour { bool isShowingDialog = false;
IEnumerator Start() { // Step 1: Ensure all core Unity asset packs are available. if (!AndroidAssetPacks.coreUnityAssetPacksDownloaded) { var corePackNames = AndroidAssetPacks.GetCoreUnityAssetPackNames(); if (corePackNames.Length > 0) { Debug.Log("Downloading core Unity asset packs..."); var coreDownload = AndroidAssetPacks.DownloadAssetPackAsync(corePackNames); yield return coreDownload; } }
// Step 2: Query the state of custom asset packs. var customPacks = new string[] { "Textures", "Audio" }; var stateOperation = AndroidAssetPacks.GetAssetPackStateAsync(customPacks); yield return stateOperation;
if (stateOperation.states == null) { Debug.LogError("Failed to retrieve asset pack states."); yield break; }
// Step 3: Download any packs that are not yet installed. var packsToDownload = new List<string>(); foreach (var state in stateOperation.states) { if (state.status == AndroidAssetPackStatus.NotInstalled) { packsToDownload.Add(state.name); } }
if (packsToDownload.Count == 0) { Debug.Log("All custom asset packs are already installed."); yield break; }
// Step 4: Download with progress monitoring. AndroidAssetPacks.DownloadAssetPackAsync( packsToDownload.ToArray(), info => { if (!isShowingDialog && (info.status == AndroidAssetPackStatus.WaitingForWifi || info.status == AndroidAssetPackStatus.RequiresUserConfirmation)) { isShowingDialog = true; AndroidAssetPacks.ShowConfirmationDialogAsync(result => { isShowingDialog = false; Debug.Log(result.consentGiven ? "User gave consent. Downloads will resume." : "User denied consent."); }); } else if (info.status == AndroidAssetPackStatus.Completed) { var path = AndroidAssetPacks.GetAssetPackPath(info.name); Debug.Log($"{info.name} is ready at: {path}"); } else if (info.status == AndroidAssetPackStatus.Failed) { Debug.LogError($"{info.name} failed: {info.error}"); } } ); } }
| Property | Description |
|---|---|
| coreUnityAssetPacksDownloaded | Checks if all core Unity asset packs are downloaded. |
| Method | Description |
|---|---|
| CancelAssetPackDownload | Cancels Android asset pack downloads. |
| DownloadAssetPackAsync | Downloads Android asset packs. |
| GetAssetPackPath | Gets the full path to the location where the device stores the assets for the Android asset pack. |
| GetAssetPackStateAsync | Queries the state of Android asset packs. |
| GetCoreUnityAssetPackNames | Gets the name of every core Unity asset pack built for this application that use either the fast-follow or on-demand delivery type. |
| RemoveAssetPack | Removes Android asset pack. |
| ShowConfirmationDialogAsync | Displays a dialog that asks the user for consent to download asset packs that require user confirmation or WiFi connection. |