Downloading large file in Unity3d using WebClient

谁说我不能喝 提交于 2019-12-19 03:46:05

问题


I am looking for any ideas regarding downloading large (100mg+) files in Unity3d using WebClient. WWW runs asynchronously and would be perfect except it returns a memory error and crashes the app, so I have moved to the solution as outlined here:

how to download a file from url and save in location using unity3d in C sharp?

This works like a dream except it shuts down all the scripts in my app until the download is complete. I cannot seem to even run a loading bar at the same time as the download is going on. I have tried handling this by adding a coroutine to handle the download, but so far, no luck.

Any help would be appreciated. My code currently (after many iterations) looks like:

C#

 void Update()
    {
    //Sets up the timer so I can use it to watch the debugging
        timer += Time.deltaTime;
        Debug.Log (timer);

//Checks to see if the file was already downloaded and saved.  If not, it begins downloading.
    if (FileExists == 0 && timer >= 5) {
        StartCoroutine ("DOWNLOAD");

    } 
//If the file already exists, it jumps straight to the next scene.
    else if (FileExists == 1) {
        Application.LoadLevelAsync ("Different_Level");
    }

    //These are the buttons.  One should stop the download if the player decides they don't want to wait.
    if (Input.GetMouseButtonUp (0)) {

        RaycastHit rc_hit;
        Ray hud_ray = Camera.main.ScreenPointToRay (Input.mousePosition);

    if (Physics.Raycast (hud_ray, out rc_hit, Mathf.Infinity, 1 << LayerMask.NameToLayer ("HUD"))) {

    if (rc_hit.transform.gameObject == b_BackButton) {

        StopCoroutine ("EN_Loop");
        Application.LoadLevelAsync ("Other_Level"); 
                            }
                    }
            }

}

//This is what should happen when the video is done downloading
void AllDone ()

{
    Debug.Log ("Download Complete");
    Application.LoadLevelAsync ("Next_Level");
}

////This is the coroutine I created int he hopes that I could get the download to run in the background.
IEnumerator DOWNLOAD()
{
    Debug.Log("downloading_EN");
    WebClient client = new WebClient();
    client.DownloadFile ("http://ServerInfo.net/moviefile.mp4", Application.persistentDataPath + "/" + "moviefile.mp4");
    yield return null;

    AllDone ();

}

回答1:


Try using WebClient.DownloadFileAsync to not lock up the main thread (so your other scripts will still run) and use the WebClient.DownloadFileCompleted event to know when it's completed.

There's no need to use a Coroutine for using the WebClient and make sure to only call this Method once:

void DownloadFile()
{
    WebClient client = new WebClient();
    client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler( DownloadFileCompleted );
    client.DownloadFileAsync ((new Uri ("http://ServerInfo.net/moviefile.mp4", Application.persistentDataPath + "/" + "moviefile.mp4"));
}

void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    if (e.Error == null)
    {
        AllDone ();
    }
}

More information on DownloadFileAsync:

https://msdn.microsoft.com/en-us/library/system.net.webclient.downloadfileasync%28v=vs.110%29.aspx

And DownloadFileCompleted Event:

https://msdn.microsoft.com/en-us/library/system.net.webclient.downloadfilecompleted%28v=vs.110%29.aspx



来源:https://stackoverflow.com/questions/29707709/downloading-large-file-in-unity3d-using-webclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!