这是我要去的代码。 我在这里找到了。 Downloading large file in Unity3d using WebClient 这似乎是最好的方法吗?另外我应该用什么iOS?
void DownloadFile()
{
loadingBar.gameObject.SetActive(true);
downloadButton.SetActive(false);
downloadingFile = true;
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(DownloadFileCompleted);
client.DownloadFileAsync(new Uri(url), Application.persistentDataPath + "/" + fileName);
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
double percentage = bytesIn/totalBytes * 100;
downloadProgressText = "Downloaded " + e.BytesReceived + " of " + e.TotalBytesToReceive;
downloadProgress = int.Parse(Math.Truncate(percentage).ToString());
totalBytes = e.TotalBytesToReceive;
bytesDownloaded = e.BytesReceived;
}
void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
AllDone();
}
}
void AllDone()
{
Debug.Log("File Downloaded");
FileExists = 1;
}
public void DeleteVideo()
{
print("Delete File");
PlayerPrefs.DeleteKey("videoDownloaded");
FileExists = 0;
enterButton.SetActive(false);
downloadButton.SetActive(true);
File.Delete(Application.persistentDataPath + "/" + fileName);
}
您正处在正确的轨道上。 * Android服务*是您需要的。您必须从Java开始提供服务,并且必须使用不带C#代码的Java代码进行下载。这被称为插件。你链接的代码是无用的。您只能将其用作写入/移植到Java的参考。当你启动服务时,你可以命令该服务下载文件。 Unity关闭时,应继续下载。对于iOS,您必须使用Object-C创建服务而不是Java。这些不是件容易的事,但谷歌搜索他们应该帮助。 – Programmer