I want to download all newest bundle when game start. But bundle can only be downloaded after Addressables.Load().
How to implement it.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
public class HotUpdateManager : MonoBehaviour
{
private bool needUpdate;
private List<string> needUpdateCatalogs;
private AsyncOperationHandle<List<IResourceLocator>> updateHandle;
private bool isUpdating;
void Start()
{
Debug.Log("Start checking if bundles need to be updated");
StartCoroutine(checkUpdate());
}
void Update()
{
if (isUpdating)
{
int progress = (int)(updateHandle.PercentComplete * 100);
Debug.Log($"Updating bundle ... {progress}%");
}
}
IEnumerator checkUpdate()
{
var init = Addressables.InitializeAsync();
yield return init;
AsyncOperationHandle<List<string>> handle = Addressables.CheckForCatalogUpdates(false);
yield return handle;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
List<string> catalogs = handle.Result;
if (catalogs != null && catalogs.Count > 0)
{
needUpdate = true;
needUpdateCatalogs = catalogs;
}
}
if (needUpdate)
{
StartCoroutine(download());
}
else
{
Debug.Log("Dont need update");
}
Addressables.Release(handle);
}
IEnumerator download()
{
Debug.Log("Start download bundles");
isUpdating = true;
// Only downlaod catalog.json
// TODO I want to download all newest bundle
updateHandle = Addressables.UpdateCatalogs(needUpdateCatalogs, false);
yield return updateHandle;
// Download finish
isUpdating = false;
Addressables.Release(updateHandle);
}
}
6194773–679525–Demo.7z (408 KB)