【问题标题】:Downloading large file in Unity3d using WebClient使用 WebClient 在 Unity3d 中下载大文件
【发布时间】:2023-03-20 21:03:01
【问题描述】:

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

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

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

任何帮助将不胜感激。我的代码目前(经过多次迭代)看起来像:

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

}

【问题讨论】:

  • 感谢您的编辑,Alex Art。

标签: c# asynchronous unity3d download webclient


【解决方案1】:

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

使用 WebClient 无需使用协程,请确保只调用该方法一次:

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 的更多信息:

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

还有 DownloadFileCompleted 事件:

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

【讨论】:

  • 嗨詹姆斯!这很出色,但 AsyncCompletedEventHandler 抛出“找不到”错误。我在脚本顶部声明了“使用 System.Net”,我是否缺少组件?
  • 我正在使用 Unity 4.5 和 MonoDevelop - Unity 4.0.1
  • 嗨,Kimberly,您想在脚本顶部添加以下声明:“using System.ComponentModel;”,我已经更新了示例,以便在需要时明确使用它。
  • 太棒了 :) 这就是诀窍!只有两件事要添加到您的编辑中,似乎我们必须在 AsyncCompleted 前面保留“新”,并且下载位置的 Uri 也需要声明为新的(我已经对您的答案进行了编辑,希望您不介意)。
  • 嗨@GuardFromUA!我没有给你的代码示例,我没有跟踪这个项目的下载。但是您应该能够检查已下载的字节数并将该数字与预期的下载大小进行比较以跟踪您的进度。
猜你喜欢
  • 2016-03-23
  • 1970-01-01
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多