我有同樣的問題。起初,我使用了一個在後臺工作的服務,並下載了我需要的文件,包括計算進度和完成事件。
然後,我讓我的插件更簡單易用。您創建一個Java對象的實例,併爲其提供響應的名稱和方法名稱。我使用json
來序列化和反序列化java和C#對象,因爲只有字符串可以在Unity的MonoBehaviour
對象和java對象之間傳遞。
這裏是downnload看起來如何在Android插件:
Uri Download_Uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
//Restrict the types of networks over which this download may proceed.
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
//Set whether this download may proceed over a roaming connection.
request.setAllowedOverRoaming(true);
//Set the local destination for the downloaded file to a path within the application's external files directory
String[] split = url.split("/");
request.setDestinationInExternalFilesDir(activity, null, split[split.length-1]);
//Set the title of this download, to be displayed in notifications (if enabled).
request.setTitle("Downloading " + title);
//Set a description of this download, to be displayed in notifications (if enabled)
request.setDescription("Downloading " + name);
request.setVisibleInDownloadsUi(false);
//Enqueue a new download and get the reference Id
long downloadReference = downloadManager.enqueue(request);
然後,你可以發回統一的參考ID,這樣你可以得到的進展,並檢查文件是否仍然在下載一旦您的應用程序已重新啓動(使用SharedPreferences \ PlayerPrefs來存儲它們)
看起來不錯,接近我的想法。但我想知道如何去超過1個文件。例如,我有500個圖像文件可供下載。我不想用500個下載進度條(如果允許下載併發文件)氾濫通知面板。是否有可能以某種方式顯示集體進步。在Unity中,我將使用文件計數來計算進度(下載的100/500文件=完成20%)。最後,如何從存儲中訪問Unity中下載的文件?對於nooby問題抱歉,因爲我沒有太多的本地開發或插件的經驗:) –
你添加'request.setNotificationVisibility(false)',然後把你自己的通知,顯示進度。 – gilgil28