0

我使用 UnityWebRequestAssetBundle.GetAssetBundle 下载位于远程服务器上的资产包。虽然在 Android 上一切正常,但在最小化应用程序(或设备进入睡眠状态)时,iOS 上的下载会停止。

当我再次将应用程序置于前台时,XCode 显示以下日志:

-> applicationWillResignActive()
-> applicationDidEnterBackground()
2020-03-08 08:37:51.571235+0100 app[3852:2943256] Can't end BackgroundTask: no background task exists with identifier 1 (0x1), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.
-> applicationWillEnterForeground()
2020-03-08 08:37:55.169337+0100 app[3852:2943564] [] nw_read_request_report [C4] Receive failed with error "Software caused connection abort"
2020-03-08 08:37:55.194339+0100 app[3852:2943564] Task <015CA857-5213-4905-8F1B-4A0E3DA330DE>.<5> HTTP load failed, 350/5095859 bytes (error code: -1005 [1:53])
2020-03-08 08:37:55.195303+0100 app[3852:2943564] Task <446A65DA-5ACF-493C-AE8F-B746364F4A9E>.<6> HTTP load failed, 693/0 bytes (error code: -1005 [1:53])
2020-03-08 08:37:55.197007+0100 app[3852:2943431] Task <015CA857-5213-4905-8F1B-4A0E3DA330DE>.<5> finished with error [-1005] Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=53, NSUnderlyingError=0x2800ced00 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x282ca0be0 [0x1e218bcf0]>{length = 16, capacity = 16, bytes = 0x10021f40c0a801300000000000000000}, _kCFStreamErrorCodeKey=53, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <015CA857-5213-4905-8F1B-4A0E3DA330DE>.<5>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <015CA857-5213-4905-8F1B-4A0E3DA330DE>.<5>"
), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=http://192.168.1.48:8000/Mobile/dlc1, NSErrorFailingURLKey=http://192.168.1.48:8000/Mobile/dlc1, _kCFStreamErrorDomainKey=1}

C#代码:

public static AssetBundleDownloadProgress DownloadBundle (string bundleName, bool forImmediateLoad, DownloadBundleFinishedDelegate finished, DownloadBundleErrorDelegate error)
 {
     UnityWebRequest wr;

     if (HashDictionaryRemote.ContainsKey(bundleName))
     {
         uint crc = CRCDictionaryRemote[bundleName];
         Hash128 hash = HashDictionaryRemote[bundleName];

         wr = UnityWebRequestAssetBundle.GetAssetBundle (SERVER_URL + bundleName, hash, crc);
     }
     else
     {
         wr = UnityWebRequestAssetBundle.GetAssetBundle (SERVER_URL + bundleName);
     }

     wr.disposeDownloadHandlerOnDispose = true;
     wr.SetRequestHeader ("Cache-Control", "no-cache, no-store, must-revalidate");
     wr.SetRequestHeader ("Pragma", "no-cache");
     wr.SetRequestHeader ("Expires", "0");

     AssetBundleDownloadProgress progress = new AssetBundleDownloadProgress(wr);

     Instance.StartCoroutine(DownloadBundleCoroutine (wr, forImmediateLoad, progress, bundleName, finished, error));

     return progress;
 }

 private static IEnumerator DownloadBundleCoroutine (UnityWebRequest wr, bool forImmediateLoad, AssetBundleDownloadProgress progress, string bundleName, DownloadBundleFinishedDelegate finished, DownloadBundleErrorDelegate error)
 {
     yield return wr.SendWebRequest();

     if (wr.isNetworkError) 
     {
         error (wr.error);
     } 
     else
     {
         AssetBundle bundle = ((DownloadHandlerAssetBundle)wr.downloadHandler).assetBundle;

         if (bundle == null) 
         {
             error.Invoke($"Error loading bundle {bundleName}, probably another bundle with same files is already loaded.");
         } 
         else
         {
             finished.Invoke(bundle, progress);

             if(!forImmediateLoad)
                 bundle.Unload(true);
         }
     }
 }

我使用 Unity 2019.2

任何帮助将不胜感激!

4

1 回答 1

0

如评论中所述,无法使用常规代码在 iOS 后台下载内容。UnityWebRequest 支持NSURLSession 的 dataTaskWithRequest。这些不能在后台运行。可以使用的是NSURLSessionTask的某些变体。正如 Brian Choi 在评论中提到的那样,在后台下载资产的应用程序使用后台获取来执行此操作,它使用这些 NSURLSessionTasks。它们直接写入磁盘,在这种情况下,下载将交给操作系统完成。

当您的应用程序在前台时,可以主动监控下载,并且它将继续在后台运行,尽管其优先级将由操作系统根据设备的网络、电池、热量和其他运行应用程序的情况进行管理。

Unity 为此目的提供了一个后台下载程序包。

https://github.com/Unity-Technologies/BackgroundDownload

为了使用它加载资产包,您必须下载到应用程序的 persistentDataPath,然后使用 AssetBundle.LoadFromFile() 来实际加载资产包。

于 2021-08-25T08:33:34.447 回答