Question by
mostafanastary · Jan 03, 2020 at 11:33 AM ·
c#listdownload
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");
}
}
Comment
Your answer
Follow this Question
Related Questions
List<>.Add : Object reference not set to an object, but nothing is null 1 Answer
C# list type not working 2 Answers
LoadScene not working 1 Answer
Tracking Long Lists and Scalability 0 Answers
List instance is always the same as the blueprint ? 0 Answers