使用WebClient在Unity3d中下载大文件

金伯利·昂格

我正在寻找有关使用WebClient在Unity3d中下载大文件(大于100mg)的想法。WWW异步运行,除了返回内存错误并使应用程序崩溃之外,它是理想的选择,因此我已按照此处概述的方法进行了处理:

如何从URL下载文件并在C Sharp中使用unity3d保存在位置?

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

任何帮助,将不胜感激。我的代码当前(经过多次迭代)如下所示:

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事件知道何时完成。

无需使用协程来使用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/zh-CN/library/system.net.webclient.downloadfileasync%28v=vs.110%29.aspx

和DownloadFileCompleted事件:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Deno中下载大文件?

来自分类Dev

如何从URL下载文件并在C Sharp中使用unity3d保存在位置?

来自分类Dev

是否可以将下载的游戏对象(使用资产包)保存为预制而不是 Unity3d 文件?

来自分类Dev

在 Android 上使用 Unity3D 读取 Midi 文件

来自分类Dev

如何在Python中下载没有MemoryError的大文件?

来自分类Dev

如何在Python中下载没有MemoryError的大文件?

来自分类Dev

使用PowerShell下载大文件

来自分类Dev

WebClient在Windows 8.1中下载损坏的文件

来自分类Dev

Unity3d使用CreateSpecificCulture

来自分类Dev

使用Unity3d制作VST

来自分类Dev

Unity3d使用CreateSpecificCulture

来自分类Dev

unity3d:如何使用 OpenFolderPanel

来自分类Dev

使用jszip在Frontend中下载大量文件

来自分类Dev

使用UpdatePanel从流中下载文件

来自分类Dev

使用JavaScript在Safari中下载文件

来自分类Dev

AWS s3在特定文件夹中下载所有文件-使用PHP SDK

来自分类Dev

使用Unity3d运行全新生成的exe文件时遇到问题

来自分类Dev

如何使用unity3d在移动设备上正确加载XML文件

来自分类Dev

将文件加载到使用Webplayer嵌入的unity3d应用程序中

来自分类Dev

使用内存流下载大文件

来自分类Dev

使用HttpClient下载Web API大文件

来自分类Dev

如何使用wget下载大文件?

来自分类Dev

使用HttpClient下载Web API大文件

来自分类Dev

使用ftp协议下载大文件

来自分类Dev

Unity:在Unity3D中使用HTTP PUT

来自分类Dev

如何使用Unity Hub安装Unity3D

来自分类Dev

如何通过ftp在python中下载大文件(具有监视和重新连接)?

来自分类Dev

如何在Java中下载大文件(大小> 50MB)

来自分类Dev

Unity3d从Textures文件夹加载精灵

Related 相关文章

热门标签

归档