Search Unity

How Can I Download Multiple Files With Unity Web Request?

Discussion in 'Scripting' started by mostafanastary, Jan 3, 2020.

  1. mostafanastary

    mostafanastary

    Joined:
    Oct 4, 2015
    Posts:
    31
    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:

    Code (CSharp):
    1. public class Tmp_FileDownloader : MonoBehaviour
    2. {
    3.      public TextMeshPro m_downloadProgress;
    4.      private UnityWebRequest uwr;
    5.      public string downloadUrl;
    6.      void Start()
    7.      {
    8.          StartCoroutine(DownloadFile());
    9.      }
    10.      IEnumerator DownloadFile()
    11.      {
    12.          var uwr = new UnityWebRequest("http://localhost/1.mp4", UnityWebRequest.kHttpVerbGET);
    13.          string path = Path.Combine(Application.persistentDataPath, "1.mp4");
    14.          uwr.downloadHandler = new DownloadHandlerFile(path);
    15.          StartCoroutine(ShowDownloadProgress(uwr));
    16.          yield return uwr.SendWebRequest();
    17.          if (uwr.isNetworkError || uwr.isHttpError)
    18.              Debug.LogError(uwr.error);
    19.          else
    20.          {
    21.              m_downloadProgress.text = (string.Format("{0:P1}", uwr.downloadProgress));
    22.              Debug.Log("File successfully downloaded and saved to " + path);
    23.          }
    24.      }
    25.      public IEnumerator ShowDownloadProgress(UnityWebRequest www)
    26.      {
    27.          while (!www.isDone)
    28.          {
    29.              Debug.Log(string.Format("Downloaded {0:P1}", www.downloadProgress));
    30.              m_downloadProgress.text = (string.Format("{0:P1}", www.downloadProgress));
    31.              yield return new WaitForSeconds(.01f);
    32.          }
    33.          Debug.Log("Done");
    34.      }
    35. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  3. mostafanastary

    mostafanastary

    Joined:
    Oct 4, 2015
    Posts:
    31
    @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?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  5. jpgordon00

    jpgordon00

    Joined:
    Jan 9, 2021
    Posts:
    25
    Last edited: Mar 3, 2021
    Argus3d likes this.
  6. DaJDC

    DaJDC

    Joined:
    Jul 24, 2021
    Posts:
    5