【问题标题】:Download large file下载大文件
【发布时间】:2018-11-14 08:04:49
【问题描述】:

通过使用UnityEngine.WWW 下载文件,我得到了错误

OverflowException:数字溢出。

我发现错误是由结构本身引起的,因为字节数组的字节数超过 int.MaxValue 可以分配的字节数(~2GB)。

返回带有www.bytes 的数组会触发错误,这意味着框架可能以其他方式存储数组。

我如何以其他方式访问下载的数据,或者是否有更大文件的替代方法?

public IEnumerator downloadFile()
{
    WWW www = new WWW(filesource);

    while(!www.isDone)
    {
        progress = www.progress;
        yield return null;
    }

    if(string.IsNullOrEmpty(www.error))
    {
        data = www.bytes; // <- Errormessage fired here
    }
}

【问题讨论】:

  • 这是什么类型的文件,您要在之后保存吗?请注意,最好也提及您的 Unity 版本
  • 它是一个视频文件 (mp4),但错误与类型无关,因为它发生在所有文件类型中。我使用版本 2017.3.1f1 我使用 File.WriteAllBytes(path, data) 保存它,但无法从 www 访问数组本身
  • 好的,但是下载后你想用它做什么?玩?保存?答案取决于那个
  • 起初我存储它并在之后加载它以进行视频播放。最高可达 2GB。

标签: c# unity3d


【解决方案1】:

新答案(Unity 2017.2 及更高版本)

UnityWebRequestDownloadHandlerFile 一起使用。 DownloadHandlerFile 类是新的,用于直接下载和保存文件,同时防止高内存使用。

IEnumerator Start()
{
    string url = "http://dl3.webmfiles.org/big-buck-bunny_trailer.webm";

    string vidSavePath = Path.Combine(Application.persistentDataPath, "Videos");
    vidSavePath = Path.Combine(vidSavePath, "MyVideo.webm");

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(vidSavePath)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(vidSavePath));
    }

    var uwr = new UnityWebRequest(url);
    uwr.method = UnityWebRequest.kHttpVerbGET;
    var dh = new DownloadHandlerFile(vidSavePath);
    dh.removeFileOnAbort = true;
    uwr.downloadHandler = dh;
    yield return uwr.SendWebRequest();

    if (uwr.isNetworkError || uwr.isHttpError)
        Debug.Log(uwr.error);
    else
        Debug.Log("Download saved to: " + vidSavePath.Replace("/", "\\") + "\r\n" + uwr.error);
}

旧答案(Unity 2017.1 及更低版本)如果您想在文件下载时访问每个字节,请使用)

这样的问题是为什么 Unity 的 UnityWebRequest 被创建但它不能直接工作,因为 WWW API 现在是在最新版本的 Unity 中的 UnityWebRequest API 之上实现的,这意味着如果你得到WWW API 的错误,您也可能会遇到与 UnityWebRequest 相同的错误。即使它有效,您也可能会在像 Android 这样的小型 ram 的移动设备上遇到问题。

要做的是使用 UnityWebRequest 的 DownloadHandlerScript 功能,它允许您以块的形式下载数据。通过分块下载数据,可以防止导致溢出错误。 WWW API 没有实现这个功能,所以必须使用UnityWebRequestDownloadHandlerScript 来分块下载数据。你可以阅读它是如何工作的here

虽然这应该可以解决您当前的问题,但您在尝试使用 File.WriteAllBytes 保存大量数据时可能会遇到另一个内存问题。使用FileStream进行保存,下载完成后关闭。

创建一个自定义UnityWebRequest,用于分块下载数据,如下所示:

using System;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;

public class CustomWebRequest : DownloadHandlerScript
{
    // Standard scripted download handler - will allocate memory on each ReceiveData callback
    public CustomWebRequest()
        : base()
    {
    }

    // Pre-allocated scripted download handler
    // Will reuse the supplied byte array to deliver data.
    // Eliminates memory allocation.
    public CustomWebRequest(byte[] buffer)
        : base(buffer)
    {

        Init();
    }

    // Required by DownloadHandler base class. Called when you address the 'bytes' property.
    protected override byte[] GetData() { return null; }

    // Called once per frame when data has been received from the network.
    protected override bool ReceiveData(byte[] byteFromServer, int dataLength)
    {
        if (byteFromServer == null || byteFromServer.Length < 1)
        {
            Debug.Log("CustomWebRequest :: ReceiveData - received a null/empty buffer");
            return false;
        }

        //Write the current data chunk to file
        AppendFile(byteFromServer, dataLength);

        return true;
    }

    //Where to save the video file
    string vidSavePath;
    //The FileStream to save the file
    FileStream fileStream = null;
    //Used to determine if there was an error while opening or saving the file
    bool success;

    void Init()
    {
        vidSavePath = Path.Combine(Application.persistentDataPath, "Videos");
        vidSavePath = Path.Combine(vidSavePath, "MyVideo.webm");


        //Create Directory if it does not exist
        if (!Directory.Exists(Path.GetDirectoryName(vidSavePath)))
        {
            Directory.CreateDirectory(Path.GetDirectoryName(vidSavePath));
        }


        try
        {
            //Open the current file to write to
            fileStream = new FileStream(vidSavePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            Debug.Log("File Successfully opened at" + vidSavePath.Replace("/", "\\"));
            success = true;
        }
        catch (Exception e)
        {
            success = false;
            Debug.LogError("Failed to Open File at Dir: " + vidSavePath.Replace("/", "\\") + "\r\n" + e.Message);
        }
    }

    void AppendFile(byte[] buffer, int length)
    {
        if (success)
        {
            try
            {
                //Write the current data to the file
                fileStream.Write(buffer, 0, length);
                Debug.Log("Written data chunk to: " + vidSavePath.Replace("/", "\\"));
            }
            catch (Exception e)
            {
                success = false;
            }
        }
    }

    // Called when all data has been received from the server and delivered via ReceiveData
    protected override void CompleteContent()
    {
        if (success)
            Debug.Log("Done! Saved File to: " + vidSavePath.Replace("/", "\\"));
        else
            Debug.LogError("Failed to Save File to: " + vidSavePath.Replace("/", "\\"));

        //Close filestream
        fileStream.Close();
    }

    // Called when a Content-Length header is received from the server.
    protected override void ReceiveContentLength(int contentLength)
    {
        //Debug.Log(string.Format("CustomWebRequest :: ReceiveContentLength - length {0}", contentLength));
    }
}

使用方法:

UnityWebRequest webRequest;
//Pre-allocate memory so that this is not done each time data is received
byte[] bytes = new byte[2000];

IEnumerator Start()
{
    string url = "http://dl3.webmfiles.org/big-buck-bunny_trailer.webm";
    webRequest = new UnityWebRequest(url);
    webRequest.downloadHandler = new CustomWebRequest(bytes);
    webRequest.SendWebRequest();
    yield return webRequest;
}

【讨论】:

  • 谢谢。我做了一些更改,但这个解决方案工作正常。谢谢!
  • 是否可以从字节中检索请求的状态码?因为如果文件不存在,它会以几个字节存储文件。我在 ReceiveData 中试过了
  • 您的意思是在保存之前检查文件是否存在于服务器上?这听起来很复杂,我认为您应该为此提出新问题。我会想办法的。
  • 在我的回答中查看我的编辑。 removeFileOnAbort 可能对新的DownloadHandlerFile API 有用。祝你好运。
  • 谢谢,我现在测试了新的答案,效果很好。谢谢!
猜你喜欢
  • 2019-01-20
  • 2011-02-07
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多