2

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");
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Rooz
  • 215
  • 3
  • 13
  • 3
    Well I don't think you should use `IEnumerator` for that. To download multiple files you should use `async`. There are a lot of tutorials about this subject. Basically you will have `async` method that will take url and it will download file. You will just create `List links` and iterate over it, for each link you will create task and store this task in `List tasks`. You can then wait until everything is downloaded or try to get states of the tasks. – Raguel Jan 02 '20 at 16:47
  • @Raguel , thanks, can you explain it with code? – Rooz Jan 02 '20 at 17:49
  • Did you ever come up with a solution to this? I'm having the same issue and so far it seems that using c# to download all files concurrently is the best bet. – Jacksonkr Apr 27 '23 at 18:33

0 Answers0