在Unity C#WWW中显示进度栏 [英] Show progress bar in Unity C# WWW

查看:178
本文介绍了在Unity C#WWW中显示进度栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以从服务器下载视频,但是我需要显示进度条,可以吗? 我知道我没有WriteAllBytes的进度条

I have this code to download videos from a server, but I need to show a progress bar, is it possible? I know that I can't have a progress bar of WriteAllBytes

 private IEnumerator DownloadStreamingVideoAndLoad(string strURL)
{
    strURL = strURL.Trim();

    Debug.Log("DownloadStreamingVideo : " + strURL);

    WWW www = new WWW(strURL);

    yield return www;

    if (string.IsNullOrEmpty(www.error))
    {

        if (System.IO.Directory.Exists(Application.persistentDataPath + "/Data") == false)
            System.IO.Directory.CreateDirectory(Application.persistentDataPath + "/Data");

        string write_path = Application.persistentDataPath + "/Data" + strURL.Substring(strURL.LastIndexOf("/"));

        System.IO.File.WriteAllBytes(write_path, www.bytes);

    }
    else
    {
        Debug.Log(www.error);

    }

    www.Dispose();
    www = null;
    Resources.UnloadUnusedAssets();
}

推荐答案

1)对于WWW进度,可以使用WWW.progress属性,

1) For WWW progress, you can use WWW.progress property, http://docs.unity3d.com/ScriptReference/WWW-progress.html, the code is like this:

private IEnumerator ShowProgress(WWW www) {
    while (!www.isDone) {
        Debug.Log(string.Format("Downloaded {0:P1}", www.progress));
        yield return new WaitForSeconds(.1f);
    }
    Debug.Log("Done");
}

private IEnumerator DownloadStreamingVideoAndLoad(string strURL)
{
    strURL = strURL.Trim();

    Debug.Log("DownloadStreamingVideo : " + strURL);

    WWW www = new WWW(strURL);

    StartCoroutine(ShowProgress(www));

    yield return www;

    // The rest of your code
}

2)如果您确实需要WriteAllBytes的进度,请分块写入文件,并报告每个进度,例如:

2) If you really want progress for WriteAllBytes, write the file in chunks, and report progress for each, for example:

private void WriteAllBytes(string fileName, byte[] bytes, int chunkSizeDesired = 4096) {
    var stream = new FileStream(fileName, FileMode.Create);
    var writer = new BinaryWriter(stream);

    var bytesLeft = bytes.Length;
    var bytesWritten = 0;
    while(bytesLeft > 0) {
        var chunkSize = Mathf.Min(chunkSizeDesired, bytesLeft);
        writer.Write(bytes, bytesWritten, chunkSize);
        bytesWritten += chunkSize;
        bytesLeft -= chunkSize;

        Debug.Log(string.Format("Saved {0:P1}", (float)bytesWritten / bytes.Length));
    }
    Debug.Log("Done writing " + fileName);
}

话虽如此,我个人都不愿意这么做-写时间与下载时间相比微不足道,您真的不需要进步.

Having said that, I personally wouldn't even bother doing it - write time is insignificant comparing to download time, you don't really need progress for that.

3)至于暂停按钮,无法用WWW类实现.通常,这不是一件容易的事,并且不适用于任何服务器.假设您使用http,则需要使用If-Range标头访问服务器(假设服务器支持此标头),以从上次停止下载的位置获取文件部分.您可以从此处开始 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.27 ,在此处

3) As for pause button, there is no way to implement it with WWW class. In general, it is not an easy thing to do and will not work with any server. Assuming you work with http, you will need to access server with If-Range header, assuming server supports this, to get file part from the position you last stopped your download. You can start from here http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.27, and here https://msdn.microsoft.com/en-us/library/system.net.webrequest%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396, and here is also some example that may help you:

在我的下载器中添加暂停和继续功能

请注意,在Unity中使用System.Net库可能无法在某些平台上使用.

Pay attention that using System.Net library in Unity may not work on some platforms.

这篇关于在Unity C#WWW中显示进度栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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