2017-08-11 65 views
0

我正在創建一個需要下載大文件的應用程序(大約650MB)。我目前正在使用這種方法來下載文件。 http://answers.unity3d.com/questions/322526/downloading-big-files-on-ios-www-will-give-out-of.html在統一應用程序暫停時繼續下載。 Android和iOS

但是,如果應用程序發送到後臺或暫停下載失敗。由於我的應用程序適用於Gear VR,因此關閉耳機會暫停播放,這樣基本上所有用戶都會發生這種情況。

我認爲答案可能與使用Android服務有關,但不知道該怎麼做,或者如果這是解決這個問題的正確方法。

+0

您正處在正確的軌道上。 * Android服務*是您需要的。您必須從Java開始提供服務,並且必須使用不帶C#代碼的Java代碼進行下載。這被稱爲插件。你鏈接的代碼是無用的。您只能將其用作寫入/移植到Java的參考。當你啓動服務時,你可以命令該服務下載文件。 Unity關閉時,應繼續下載。對於iOS,您必須使用Object-C創建服務而不是Java。這些不是件容易的事,但谷歌搜索他們應該幫助。 – Programmer

回答

0

這是我要去的代碼。 我在這裏找到了。 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); 
} 
相關問題