Created
July 8, 2023 14:58
-
-
Save vbashiri/266bc7b7e7d41723fec13b1eff68ccf5 to your computer and use it in GitHub Desktop.
AssetDeliveryExtension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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