Skip to content

Instantly share code, notes, and snippets.

@vbashiri
Created July 8, 2023 14:58
Show Gist options
  • Save vbashiri/266bc7b7e7d41723fec13b1eff68ccf5 to your computer and use it in GitHub Desktop.
Save vbashiri/266bc7b7e7d41723fec13b1eff68ccf5 to your computer and use it in GitHub Desktop.
AssetDeliveryExtension
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
using Google.Play.AssetDelivery;
public class AssetDeliveryExtension
{
private const string assetBundleName = "instantbundle";
public static Action<float, AssetDeliveryStatus> OnDownloadStatus;
public static Action<AssetDeliveryErrorCode> OnAssetLoadFail;
private static AssetBundle instantBundle = null;
private static bool assetDownloadRoutineRunning = false;
public static void AssetDownloader(MonoBehaviour monoBehaviour)
{
monoBehaviour.StartCoroutine(AssetDownloadRoutine());
}
public static IEnumerator AssetDownloadRoutine()
{
PlayAssetBundleRequest bundleRequest = PlayAssetDelivery.RetrieveAssetBundleAsync(assetBundleName);
var startTime = Time.realtimeSinceStartup;
while (!bundleRequest.IsDone) {
OnDownloadStatus?.Invoke(bundleRequest.DownloadProgress, bundleRequest.Status);
yield return null;
}
if (bundleRequest.Error == AssetDeliveryErrorCode.NoError)
{
instantBundle = bundleRequest.AssetBundle;
}
else
{
Debug.Log("error downloading asset: " + bundleRequest.Error);
OnAssetLoadFail?.Invoke(bundleRequest.Error);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment