Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


DownloadAssetPackAsync

Downloads Android asset packs.
Read time 2 minutesLast updated 10 days ago

Definition

DownloadAssetPackAsync(string[], Action<AndroidAssetPackInfo>)

Downloads Android asset packs.
public static void DownloadAssetPackAsync(string[] assetPackNames, Action<AndroidAssetPackInfo> callback)

Parameters

assetPackNames

The array of names of Android asset packs to download.

The callback method to inform about download progress. It gets called multiple times for each asset pack during its download. The callback method must have a parameter of AndroidAssetPackInfo type. The default value is null.

Remarks

This method directly wraps Google's PlayCore plug-in API. If the PlayCore plug-in is missing, calling this method throws an
InvalidOperationException
.

Examples

using UnityEngine;using UnityEngine.Android;public class DownloadAssetPackCallbackExample : MonoBehaviour{ bool isShowingDialog = false; void Start() { AndroidAssetPacks.DownloadAssetPackAsync( new string[] { "MyAssetPack" }, OnDownloadProgress ); } void OnDownloadProgress(AndroidAssetPackInfo info) { switch (info.status) { case AndroidAssetPackStatus.Pending: Debug.Log($"{info.name}: Pending"); break; case AndroidAssetPackStatus.Downloading: float percentComplete = info.size > 0 ? (float)info.bytesDownloaded / info.size * 100f : 0f; Debug.Log($"{info.name}: Downloading {percentComplete:F1}%"); break; case AndroidAssetPackStatus.Transferring: Debug.Log($"{info.name}: Transferring {(info.transferProgress * 100f):F1}%"); break; case AndroidAssetPackStatus.Completed: var path = AndroidAssetPacks.GetAssetPackPath(info.name); Debug.Log($"{info.name}: Completed. Path: {path}"); break; case AndroidAssetPackStatus.Failed: Debug.LogError($"{info.name}: Failed with error {info.error}"); break; case AndroidAssetPackStatus.WaitingForWifi: case AndroidAssetPackStatus.RequiresUserConfirmation: if (!isShowingDialog) { isShowingDialog = true; Debug.Log(info.status == AndroidAssetPackStatus.WaitingForWifi ? $"{info.name}: Download paused until connected to Wi-Fi." : $"{info.name}: Download requires user confirmation."); AndroidAssetPacks.ShowConfirmationDialogAsync(result => { isShowingDialog = false; Debug.Log(result.consentGiven ? "User gave consent. Downloads will resume." : "User denied consent."); }); } break; case AndroidAssetPackStatus.Canceled: Debug.Log($"{info.name}: Download canceled."); break; case AndroidAssetPackStatus.NotInstalled: Debug.Log($"{info.name}: Android asset pack is not installed"); break; default: Debug.Log($"{info.name}: Status {info.status}"); break; } }}

DownloadAssetPackAsync(string[])

Downloads Android asset packs to the device.
public static DownloadAssetPackAsyncOperation DownloadAssetPackAsync(string[] assetPackNames)

Parameters

assetPackNames

The array of names of Android asset packs to download.

Returns

Type

Description

DownloadAssetPackAsyncOperationAn object that represents the download operation. If you yield this object inside a coroutine, the coroutine pauses until the operation is complete.

Remarks

This method directly wraps Google's PlayCore plug-in API. If the PlayCore plug-in is missing, calling this method throws an
InvalidOperationException
.

Examples

using System.Collections;using UnityEngine;using UnityEngine.Android;public class DownloadAssetPackAsyncExample : MonoBehaviour{ IEnumerator Start() { var assetPackNames = new string[] { "MyAssetPack" }; var downloadOperation = AndroidAssetPacks.DownloadAssetPackAsync(assetPackNames); // Yield until the download completes. yield return downloadOperation; if (downloadOperation.downloadedAssetPacks.Length == 0) { Debug.LogWarning("No asset packs were downloaded."); } foreach (var packName in downloadOperation.downloadedAssetPacks) { var path = AndroidAssetPacks.GetAssetPackPath(packName); Debug.Log($"Asset pack downloaded to: {path}"); } foreach (var failedPack in downloadOperation.downloadFailedAssetPacks) { Debug.LogError($"Failed to download asset pack: {failedPack}"); } }}