這是我要去的代碼。 我在這裏找到了。 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