1)对于WWW进度,可以使用WWW.progress属性,http://docs.unity3d.com/ScriptReference/WWW-progress.html http://docs.unity3d.com/ScriptReference/WWW-progress.html,代码是这样的:
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
,分块写入文件,并报告每个块的进度,例如:
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);
}
话虽如此,我个人甚至不会费心这样做 - 与下载时间相比,写入时间微不足道,您实际上并不需要为此取得进展。
3)对于暂停按钮,没有办法用WWW类来实现。一般来说,这不是一件容易的事情,并且不适用于任何服务器。假设您使用 http,您将需要使用 If-Range 标头访问服务器(假设服务器支持此功能),以从您上次停止下载的位置获取文件部分。你可以从这里开始http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.27 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.27, 和这里,这里还有一些可能对您有帮助的示例:
在我的下载器中添加暂停和继续功能 https://stackoverflow.com/questions/15995705/adding-pause-and-continue-ability-in-my-downloader
请注意,在 Unity 中使用 System.Net 库可能无法在某些平台上运行。