使用 WebClient 在 Unity3d 中下载大文件 [英] Downloading large file in Unity3d using WebClient

查看:41
本文介绍了使用 WebClient 在 Unity3d 中下载大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关使用 WebClient 在 Unity3d 中下载大型 (100mg+) 文件的任何想法.WWW 以异步方式运行,除了返回内存错误并使应用程序崩溃之外,它会很完美,因此我已转向此处概述的解决方案:

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:

如何从 url 下载文件并使用 CSharp 中的 unity3d 保存在位置?

这就像做梦一样除了它会关闭我的应用程序中的所有脚本,直到下载完成.我什至无法在下载的同时运行加载栏.我已经尝试通过添加一个协程来处理下载来处理这个问题,但到目前为止,没有运气.

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 ();

}

推荐答案

尝试使用 WebClient.DownloadFileAsync 不锁定主线程(因此您的其他脚本仍将运行)并使用 WebClient.DownloadFileCompleted 事件来知道它何时完成.

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.

使用 WebClient 不需要使用协程,并确保只调用一次此方法:

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 ();
    }
}

有关 DownloadFileAsync 的更多信息:

More information on DownloadFileAsync:

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

和 DownloadFileCompleted 事件:

And DownloadFileCompleted Event:

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

这篇关于使用 WebClient 在 Unity3d 中下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆