// 开始加载 protected override void StartLoad() { string path = BundleLoader.GetAssetBundlePath(AssetPath); m_request = UnityWebRequest.Get(path); m_request.SendWebRequest(); }
protected override void StartLoad() { string path = BundleLoader.GetAssetPathBySyncLoad(AssetPath); try { Bundle = AssetBundle.LoadFromFile(path); } catch (Exception ex) { // 加载失败后尝试下载 UnityEngine.Debug.Log(string.Format("Load asset failed, asset: {0}, exception: {1}", AssetPath, ex.Message)); TryToDownload(); return; } OnSucceed(); }
IEnumerator EtorDownload() { /* * 总结: * 自己写的超时逻辑,超时一定要用 Abort() 来终结,调用 Abort 后会触发错误,但有时却不触发错误,此时可能会导致协和永远死循环,因此用 time out 来做第二层保护 */ using (UnityWebRequest uwrItem = UnityWebRequest.Get(Url)) { int uwrTimeout = Mathf.Max(60, BundleObj.Length / AllowMinSpeed); uwrItem.timeout = uwrTimeout; uwrItem.SendWebRequest(); float blockTimer = TimeOut; float timer = uwrTimeout + 30; ulong downloadedBytes = 0; while (!uwrItem.isDone) { if (timer < 0) { uwrItem.Abort(); OnFailed("Manual timeout"); yield break; } else { timer -= Time.deltaTime; } if (uwrItem.isHttpError || uwrItem.isNetworkError) { OnFailed("Time out, " + uwrItem.error); yield break; } if (downloadedBytes == uwrItem.downloadedBytes) { // 检测是否超时 //Debug.Log("block time: " + blockTimer); if (blockTimer < 0) { uwrItem.Abort(); } else { blockTimer -= Time.deltaTime; } } else { downloadedBytes = uwrItem.downloadedBytes; blockTimer = TimeOut; //Debug.Log(string.Format("#1 {0} {1} {2}", uwrItem.downloadedBytes, DownloadedBytes, item.Name)); } yield return(null); } if (uwrItem.isHttpError || uwrItem.isNetworkError) { OnFailed(uwrItem.error); yield break; } // 写入磁盘 string localPath = BundleLoader.GetAssetBundlePersistPath(BundleObj.Name); string writeError; bool writeSucceed = WriteBytes(localPath, uwrItem.downloadHandler.data, out writeError); if (writeSucceed) { OnSucceed(); } else { OnFailed(uwrItem.error); } } }