Version: Unity 6.6 Alpha (6000.6)
LanguageEnglish
  • C#

AndroidAssetPacks.DownloadAssetPackAsync

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static DownloadAssetPackAsyncOperation DownloadAssetPackAsync(string[] assetPackNames);

Parameters

Parameter Description
assetPackNames The array of names of Android asset packs to download.

Returns

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

Description

Downloads Android asset packs.

This method directly wraps Google's PlayCore plugin API. If the PlayCore plugin is missing, calling this method throws an InvalidOperationException exception. Additional resources: DownloadAssetPackAsyncOperation, AndroidAssetPackInfo, AndroidAssetPacks.CancelAssetPackDownload, AndroidAssetPacks.GetAssetPackPath, AndroidAssetPacks.GetAssetPackStateAsync, AndroidAssetPacks.RemoveAssetPack, AndroidAssetPacks.RequestToUseMobileDataAsync.

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}"); } } }

Declaration

public static void DownloadAssetPackAsync(string[] assetPackNames, Action<AndroidAssetPackInfo> callback);

Parameters

Parameter Description
assetPackNames The array of names of Android asset packs to download.
callback 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.

Description

Downloads Android asset packs.

This method directly wraps Google's PlayCore plugin API. If the PlayCore plugin is missing, calling this method throws an InvalidOperationException exception. Additional resources: AndroidAssetPackInfo, AndroidAssetPacks.CancelAssetPackDownload, AndroidAssetPacks.GetAssetPackPath, AndroidAssetPacks.GetAssetPackStateAsync, AndroidAssetPacks.RemoveAssetPack, AndroidAssetPacks.RequestToUseMobileDataAsync.

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; } } }