1

我想使用 unitywebrequest 从服务器下载多个具有不同扩展文件类型的文件,并使用进度条显示进度。也许我需要一个列表或字典来添加我的文件 URL,但是如何?我可以通过以下代码使用一个文件来做到这一点:

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");
    }
}
4

0 回答 0