c#webClient.DownloadFile不';t下载,它只是创建一个空文本文件
我想从Pastebin(raw)下载一个文本到一个文本文件中。 我创建了一个c#webClient.DownloadFile不';t下载,它只是创建一个空文本文件,c#,unity3d,download,webclient,pastebin,C#,Unity3d,Download,Webclient,Pastebin,我想从Pastebin(raw)下载一个文本到一个文本文件中。 我创建了一个IEnumerator,但不知怎的,它只是创建了一个空文本文件 public IEnumerator DownloadTextFile() { WebClient version = new WebClient(); yield return version; version.DownloadFileAsync(new Uri(myLink), "version.txt"); } public
IEnumerator
,但不知怎的,它只是创建了一个空文本文件
public IEnumerator DownloadTextFile()
{
WebClient version = new WebClient();
yield return version;
version.DownloadFileAsync(new Uri(myLink), "version.txt");
}
public IEnumerator DownloadTextFile()
{
WebClient version = new WebClient();
yield return version;
version.DownloadFile(myLink , "version.txt");
}
提前感谢。请确保将您的WebClient放在using语句中,以便它能够正确处理并确保下载完成。对于异步版本,您需要确保在忙碌时不会退出
//Method 1
using (var version = new WebClient())
version.DownloadFile("https://pastebin.com/raw/c1GrSCKR", @"c:\temp\version.txt");
// Method 2 (better if you are working within Task based async system)
//using (var version = new WebClient())
// await version.DownloadFileTaskAsync("https://pastebin.com/raw/c1GrSCKR", @"c:\temp\version.txt");
// Method 3 - you can't dispose till is completed / you can also register to get notified when download is done.
using (var version = new WebClient())
{
//version.DownloadFileCompleted += (object sender, AsyncCompletedEventArgs e) =>
//{
//};
version.DownloadFileAsync(new Uri("https://pastebin.com/raw/c1GrSCKR"), @"c:\temp\version.txt");
while (version.IsBusy) { }
}
WebClient不是设计用于Unity3D内部的,
yield return版本代码>不等待下载文件
您可以使用该类并以这种方式执行下载WWW
是Unity的一部分,旨在以Unity的工作方式工作
public IEnumerator DownloadTextFile()
{
WWW version = new WWW(myLink);
yield return version;
File.WriteAllBytes("version.txt", version.bytes);
//Or if you just wanted the text in your game instead of a file
someTextObject.text = version.text;
}
请务必通过调用startcroutine(DownloadTextFile())
启动下载文本文件,Scott Chamberlain的方法是正确的,并且推荐的统一方法,因为WWW
API在后台处理线程问题。您只需使用coroutine下载它,然后使用Scott提到的File.writealxxx
函数之一手动保存它
我之所以添加这个答案,是因为这个问题专门询问WebClient
,而且有时使用WebClient
也可以,比如大数据
问题是您正在生成WebClient
。您不必像以前那样在协同程序中生成WebClient
。只需订阅它的DownloadFileCompleted
事件,该事件将在另一个线程上调用。为了在DownloadFileCompleted
回调函数中使用Unity函数,您必须使用post中的UnityThread
脚本,并在UnityThread.executeInUpdate
函数的帮助下执行Unity函数
下面是一个完整的示例(需要):
注意,这是Unity3d,所以而(version.IsBusy){}
是一个很大的禁忌,将锁定游戏。您应该在(version.IsBusy){yield return 0;}
时执行,这样它会等待一帧,然后再次检查它是否忙。很好,请致电Scott。我完全忽略了统一部分。你的帖子很到位。谢谢你的快速回复。它工作得很好。有没有办法查看下载进度?是的,如果您查看WWW的文档,您将看到该字段。阅读这篇文章,看看进步有多大。
public Text text;
int fileID = 0;
void Awake()
{
UnityThread.initUnityThread();
}
void Start()
{
string url = "http://www.sample-videos.com/text/Sample-text-file-10kb.txt";
string savePath = Path.Combine(Application.dataPath, "file.txt");
downloadFile(url, savePath);
}
void downloadFile(string fileUrl, string savePath)
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DoSomethingOnFinish);
webClient.QueryString.Add("fileName", fileID.ToString());
Uri uri = new Uri(fileUrl);
webClient.DownloadFileAsync(uri, savePath);
fileID++;
}
//THIS FUNCTION IS CALLED IN ANOTHER THREAD BY WebClient when download is complete
void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e)
{
string myFileNameID = ((System.Net.WebClient)(sender)).QueryString["fileName"];
Debug.Log("Done downloading file: " + myFileNameID);
//Ssafety use Unity's API in another Thread with the help of UnityThread
UnityThread.executeInUpdate(() =>
{
text.text = "Done downloading file: " + myFileNameID;
});
}