我目前正在開發一款益智遊戲,並試圖製作一個自動更新程序。所以用戶可以在不手動下載最新版本的情況下更新他們的遊戲。版本檢查本身正在工作,但是當我試圖使用WWW(url)下載文件時,需要花費相當多的時間來下載,但是一旦我檢入了我的文件瀏覽器,它就創建了一個0字節的文件。而在OSX中,它實際上被立即刪除。File.WriteAllBytes正在創建一個0字節文件
這是我正在使用的代碼。
void UpdateGame(){
string path = Application.dataPath;
if (Application.platform == RuntimePlatform.OSXPlayer) {
path += "/../../";
}
else if (Application.platform == RuntimePlatform.WindowsPlayer) {
path += "/../";
}
if (System.IO.File.Exists (path+"/Apo_Alpha.app")) {
Debug.Log ("File exists!");
System.IO.File.Delete (path+"/Apo_Alpha.app");
}
WWW www = new WWW ("https://www.dropbox.com/sh/aayo9iud7t98hgb/AACDqSST_-rT2jIfxq1Zc2SXa?dl=1");
while (!www.isDone) {
Debug.Log ("Waiting for download");
}
string fullPath = path+"/Apo_Alpha.app";
System.IO.File.WriteAllBytes (fullPath, www.bytes);
Debug.Log ("Done downloading...");
Debug.Log ("File downloaded at " + fullPath);
}
這是怎麼發生的?是否因爲我可能不得不等待這些字節被寫入?我很想在這裏獲得某種形式的幫助。我相信這只是一個小錯誤,但我無法在任何地方找到答案。 (URL的是正確的,我雙重檢查他們...)
我不知道團結好,但'isDone'文件明確地說:「你不應該寫循環直到下載完成;使用協程代替。「 –
只是一個註釋(與您的問題無關):檢查'File.Exist'不能確保它在下一行執行後仍然存在,或者您有適當的權限將其刪除。你仍然需要捕捉異常。 – Jens