Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Download Progress bar

Discussion in 'Scripting' started by Tikas3, Feb 12, 2019.

  1. Tikas3

    Tikas3

    Joined:
    Jan 24, 2019
    Posts:
    9
    Hello all,

    I was wondering if someone could tell me how I would use a while loop in the IEnumerator (or if this is achievable in general). It doesnt seem to work. Here is the explenation on the site:

    Code (CSharp):
    1.  
    2.    IEnumerator DownloadFile() {
    3.        var uwr = new UnityWebRequest("https://unity3d.com/", UnityWebRequest.kHttpVerbGET);
    4.        string path = Path.Combine(Application.persistentDataPath, "unity3d.html");
    5.        uwr.downloadHandler = new DownloadHandlerFile(path);
    6.        yield return uwr.SendWebRequest();
    7.        if (uwr.isNetworkError || uwr.isHttpError)
    8.            Debug.LogError(uwr.error);
    9.        else
    10.            Debug.Log("File successfully downloaded and saved to " + path);
    11.    }
    12.  
    and my own code

    Code (CSharp):
    1.     IEnumerator DownloadIndividualFiles()
    2.     {
    3.  
    4.         for (int i = 0; i < downloadList.Count; i++)
    5.         {
    6.             string fileName = downloadList[i].ToString().Trim();
    7.  
    8.             Debug.Log("Starting Individual Download");
    9.  
    10.             string rawDataFolder = "disclosed";
    11.             string url = (rawDataFolder + fileName).ToString();
    12.  
    13.             UnityWebRequest filewww = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
    14.             Debug.Log("Downloading" + url);
    15.             filewww.downloadHandler = new DownloadHandlerFile(savePath + fileName);
    16.  
    17.             yield return filewww.SendWebRequest();
    18.  
    19.             while (!filewww.isDone)
    20.             {
    21.                 progressBar.value = filewww.downloadProgress;
    22.            
    23.             }
    24.  
    25.             if (filewww.isNetworkError || filewww.isHttpError)
    26.             {
    27.                 Debug.LogError(filewww.error);
    28.                 errorOccured = true;
    29.             }
    30.             else
    31.             {
    32.                 Debug.Log("File successfully downloaded and saved to " + savePath);
    33.             }
    34.  
    35.             if (errorOccured == true)
    36.             {
    37.                 break;
    38.             }
    39.         }
    40.         if (errorOccured == false)
    41.         {
    42.             Debug.Log("Succes");
    43.         }
    44.  
    45.  
    46.    
    47.     }
    48.  
    49.  
    50. }
    I want to use a slider (progressBar) to represent the download progress. Any suggestions?

    Greets,
    Tikas
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,642
    This line:
    yield return filewww.SendWebRequest();

    will suspend coroutine until request is finished. If you want to track the progress, simply do
    filewww.SendWebRequest();

    and then in the while loop below it do

    yield return null;

    yielding null will suspend coroutine until next frame.
     
  3. Tikas3

    Tikas3

    Joined:
    Jan 24, 2019
    Posts:
    9
    Hi Aurimas,

    Thanks for your response. I was aware of this, and actually tried what you suggested earlier, but this seemed to fail (maybe for another reason though, although what does not come to mind). I circumvented this by updating the progressBar.value in the update class (which in the end was better for me because I am calling progressBar.value from other functions).

    Still, gonna see if your method works none the less. Thanks again!

    Greets,
    Tikas
     
  4. MacLeanMX

    MacLeanMX

    Joined:
    Feb 20, 2018
    Posts:
    1
    Hello, @Tikas3

    Do you have a solution?
     
  5. White_Wabbit

    White_Wabbit

    Joined:
    Aug 16, 2019
    Posts:
    10
    Try:

    Code (CSharp):
    1. IEnumerator WatForResponse(UnityWebRequest request)
    2. {
    3.        while (!request.isDone)
    4.        {
    5.                 progressBar.value = request.downloadProgress;
    6.                 yield return new WaitForSeconds(0.2f);
    7.        }
    8. }
    and put:

    StartCoroutine(WatForResponse(filewww));

    before :

    yield return filewww.SendWebRequest();
     
  6. Sonercali

    Sonercali

    Joined:
    Nov 18, 2018
    Posts:
    1
    Thanks it worked

    here is my code....



    Code (CSharp):
    1. UnityWebRequest www = UnityWebRequest.Get(url);
    2.         DownloadHandler handle = www.downloadHandler;
    3.         slider.gameObject.SetActive(true);
    4.         loadingText.gameObject.SetActive(true);
    5.         //Send Request and wait
    6.         www.SendWebRequest();
    7.         while (!www.isDone)
    8.         {
    9.             slider.value = www.downloadProgress;
    10.             loadingText.text ="Downloading... "+ (int)(www.downloadProgress * 100f) + "%";
    11.            
    12.             yield return null;
    13.          
    14.         }
    15.             slider.gameObject.SetActive(false);
    16.             loadingText.gameObject.SetActive(false);