How Can I Download Multiple Files With Unity Web Request?

I want to download multiple files with different extension file types from the server with unitywebrequest and show progress with progress bar. maybe I need a list or dictionary to add my files URL, but how? I can do it with one file by this code:

public class Tmp_FileDownloader : MonoBehaviour
{
     public TextMeshPro m_downloadProgress;
     private UnityWebRequest uwr;
     public string downloadUrl;
     void Start()
     {
         StartCoroutine(DownloadFile());
     }
     IEnumerator DownloadFile()
     {
         var uwr = new UnityWebRequest("http://localhost/1.mp4", UnityWebRequest.kHttpVerbGET);
         string path = Path.Combine(Application.persistentDataPath, "1.mp4");
         uwr.downloadHandler = new DownloadHandlerFile(path);
         StartCoroutine(ShowDownloadProgress(uwr));
         yield return uwr.SendWebRequest();
         if (uwr.isNetworkError || uwr.isHttpError)
             Debug.LogError(uwr.error);
         else
         {
             m_downloadProgress.text = (string.Format("{0:P1}", uwr.downloadProgress));
             Debug.Log("File successfully downloaded and saved to " + path);
         }
     }
     public IEnumerator ShowDownloadProgress(UnityWebRequest www)
     {
         while (!www.isDone)
         {
             Debug.Log(string.Format("Downloaded {0:P1}", www.downloadProgress));
             m_downloadProgress.text = (string.Format("{0:P1}", www.downloadProgress));
             yield return new WaitForSeconds(.01f);
         }
         Debug.Log("Done");
     }
}

Try passing DownloadFile a string for the URL and the file name instead of hard coding them into the method, and call DownloadFile for each file you want to download.

Also you declare uwr in your class but never use it, as the first thing you do in DownloadFile is declare a local variable of the same name anyways. Not sure what that is about.

@Joe-Censored thanks, actually i want show the popup to show like this: Downloaded File 1 Of 5 with Progress bar.
i don’t know how do it?

Displaying download progress will be an entirely different issue than getting the downloads working, and without knowing how much you understand the UI system it is impossible to tell you in a forum post. I suggest following some Unity UI tutorials and then if you get stuck ask more specific UI questions.

@mostafanastary I made something to do this. You can download any amount of files all with different extension types (if you choose to do so). It supports simple callbacks, progress calculation, atomic file downloading / cleanup, and more.

https://github.com/jpgordon00/UnityGroupDownloader

1 Like

u can use my AdvRequest class: https://github.com/JDC-Unity/-UNITY-AdvancedDownload