How to get download progress using UnityWebRequest to download Asset Bundle from server?

10,415

Instead of

yield return StartCoroutine(DownloadProgress(operation));

the proper way of yielding an IEnumerator is simply

yield return DownloadProgress(operation);

However why not simply do it directly in the same Coroutine?

I would however recommend to rather use UnityWebRequestAssetBundle.GetAssetBundle instead of configurating it from scratch yourself and some other changes:

IEnumerator DownloadAsset()
{
    string url = "<here the URL for asset bundle>";
    
    /*
     * directly use UnityWebRequestAssetBundle.GetAssetBundle
     * instead of "manually" configure and attach the download handler etc
     */
    using (var uwr = new UnityWebRequestAssetBundle.GetAssetBundle(url, 36, 0)
    {
        var operation = uwr.SendWebRequest();

        /* 
         * this should be done only once actually 
         */
        progressBar.color = Color.red;

        while (!operation.isDone)
        {
            /* 
             * as BugFinder metnioned in the comments
             * what you want to track is uwr.downloadProgress
             */
            downloadDataProgress = uwr.downloadProgress * 100;

            /*
             * use a float division here 
             * I don't know what type downloadDataProgress is
             * but if it is an int than you will always get 
             * an int division <somethingSmallerThan100>/100 = 0
             */
            progressBar.fillAmount = downloadDataProgress / 100.0f;

            print("Download: " + downloadDataProgress);
            yield return null;
        }

        AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
        {
            print("Get asset from bundle...");
        }

        /* 
         * You do not have to Dispose uwr since the using block does this automatically 
         */
        //uwr.Dispose();

        //Load scene
        print("ready to Load scene from asset...");
        StartCoroutine(LoadSceneProgress("Example"));
        bundle.Unload(false);
    }
}

Note from Mayur Asodariya fromt he comments below:

It might happen that your server does not provide the download size and therefore no progress information. In this case you can follow this post to configure your server correctly.

Share:
10,415

Related videos on Youtube

Sharafiq Sharif
Author by

Sharafiq Sharif

Updated on June 04, 2022

Comments

  • Sharafiq Sharif
    Sharafiq Sharif almost 2 years

    I still new using UnityWebRequest to download and load asset bundle from server container. The problem is the value for the download progress always 0. How can I get the value for download progress?

    Code Below what I try to download and get the download progress.

    //Method to download the assetbundle
    IEnumerator DownloadAsset()
    {
        string url = here the URL for asset bundle;
        using (var uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET))
        {
            uwr.downloadHandler = new DownloadHandlerAssetBundle(url, 36, 0);
            UnityWebRequestAsyncOperation operation = uwr.SendWebRequest();
            yield return StartCoroutine(DownloadProgress(operation));
    
            AssetBundle bundle = DownloadHandlerAssetBundle.GetContent(uwr);
            {
                print("Get asset from bundle...");
            }
    
    
            //Load scene
            uwr.Dispose();
            print("ready to Load scene from asset...");
            StartCoroutine(LoadSceneProgress("Example"));
            bundle.Unload(false);
        }
    }
    
    //Method for download progress
    IEnumerator DownloadProgress(UnityWebRequestAsyncOperation operation)
    {
        while (!operation.isDone)
        {
            progressBar.color = Color.red;
            downloadDataProgress = operation.progress * 100;
            progressBar.fillAmount = downloadDataProgress / 100;
            print("Download: " + downloadDataProgress);
            yield return null;
        }
        Debug.Log("Done");
    }
    

    I expect to display download progress bar or download percentage to show the download progress on screen. but the download progress value always 0.

    • BugFinder
      BugFinder almost 5 years
      Would you not be tracking uwr.downloadProgress?
    • mowie2
      mowie2 almost 5 years
      This post might be able to help you stackoverflow.com/questions/48849565/…
    • Sharafiq Sharif
      Sharafiq Sharif almost 5 years
      @BugFinder I want tracking the download progress but my code not work.
    • Sharafiq Sharif
      Sharafiq Sharif almost 5 years
      @mowie2 I dunno why but I already try to follow the dicumentation but still the download progress value always 0.
    • BugFinder
      BugFinder almost 5 years
      As I said it should be uwr.downloadProgress which has the download progress status in it.
  • Mayur Asodariya
    Mayur Asodariya over 3 years
    I tried above way, But still not able to get percentage in my android device(downloadProgress is always return 0), However in my windows editor above code is working fine. If there is any otherway then please guide me
  • Mayur Asodariya
    Mayur Asodariya over 3 years
    I found that the issues was from server side and now I able to get float number by downloadprogress. For more info link
  • derHugo
    derHugo over 3 years
    @MayurAsodariya thanks for this input! Added it to the answer
  • Arcueid D'athemon
    Arcueid D'athemon over 2 years
    "an int division <somethingSmallerThan100>/100 = 0" because "something / int = int"