1

I am trying to download a prefab using the Unity Addressable system. The address is located on a remote server and I have my addressable system set to pull from that remote server. The code below loads that asset from the server and is supposed to report its download progress. However, this doesn't seem to work. The UpdateProgressBar method only gets called once and then never again. As far as I know, Coroutines are supposed to run as long as they have something to do, so I assumed using the while loop would cause the coroutine to continue to call the UpdateProgressBar method.

I looked around on the internet and it does seem like people have had similar issues with getting download progress from AsyncOperationHandles, but most of those issues were a few years old, so I'd assume they'd be fixed by now.

Anyways, is there anything I'm missing/doing wrong? I'm pretty new to Unity Addressables, so any tips or constructive criticism would be most welcome.

    public IEnumerator DownloadAsset(string assetKey)
    {
        loadingScreen.SetActive(true);
        AsyncOperationHandle<GameObject> handle = Addressables.LoadAssetAsync<GameObject>(assetKey);
        handle.Completed += (AyncOperationHandle) =>
        {
            DownloadComplete(handle);
            loadingScreen.SetActive(false);
        };

        yield return handle;
        DownloadStatus downloadStatus = handle.GetDownloadStatus();
        while (!handle.IsDone && downloadStatus.Percent < 1)
        {
            UpdateProgressBar(downloadStatus.Percent);
            yield return null;
        }
    }

    private void DownloadComplete(AsyncOperationHandle goHandle)
    {
        Debug.Log("Asset Downloaded!");
        GameObject obj = goHandle.Result as GameObject;
        Instantiate(obj);

        Addressables.Release(goHandle);
    }

    public void UpdateProgressBar(float progress)
    {
        progressBar.normalizedValue = progress;
        Debug.Log(string.Format("Downloaded {0:P1}", progress));

        if (progress >= 1.0f) loadingScreen.SetActive(false);
    }

The DowloadAsset function is being called from another script when a button is clicked:

    [SerializeField] private string assetKey;

    void Start()
    {
        Button button = GetComponent<Button>();
        button.onClick.AddListener(() => StartCoroutine(gameManager.DownloadAsset(assetKey)));
    }
Knight Steele
  • 169
  • 4
  • 14
  • Have you tried it without the `yield return handle;` ? I think you already wait there until the operation is done – derHugo Jan 27 '22 at 21:01
  • I have, yeah. The result ended up being the same. I also tried various different yield returns in place of the yield return null, such as WaitForSeconds, WaitForEndOfFrame, etc. but these also did not work. – Knight Steele Jan 27 '22 at 21:54
  • `The UpdateProgressBar method only gets called once and then never again.` .. with what value of progress does that happen? – derHugo Jan 28 '22 at 05:26
  • The one time it gets called, the value is at 0. – Knight Steele Jan 28 '22 at 20:42

2 Answers2

0

Try this

public IEnumerator DownloadAsset(string assetKey)
{
    loadingScreen.SetActive(true);
    var downloadAsync = Addressables.DownloadDependenciesAsync(assetKey);
    downloadAsync.Completed += (AyncOperationHandle) =>
    {
        DownloadComplete(handle);
        loadingScreen.SetActive(false);
    };

    yield return downloadAsync;
    
    while (!downloadAsync.IsDone && downloadAsync.IsValid())
    { 
        DownloadStatus downloadStatus = downloadAsync.GetDownloadStatus();

        UpdateProgressBar(downloadStatus.Percent);

        yield return null;
    }
}
0
private IEnumerator ShowSizeText(string labelName)
    {

        AsyncOperationHandle<long> asynOperCheck = Addressables.GetDownloadSizeAsync(labelName);
        yield return asynOperCheck;

        if (asynOperCheck.Status == AsyncOperationStatus.Succeeded)
        {
            long DownloadSize = asynOperCheck.Result;
            long size = (long)Math.Round(DownloadSize / 1000000f, 2);
            long totalBundleSize = (long)size;
        }
        else if (asynOperCheck.Status == AsyncOperationStatus.Failed)
        {
            Debug.Log("Network Error");
        }
    }
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83