void LoadNext() { if (requestList.Count < 1) { state = BundleLoadState.Success; totalLength = 0; loadIndex = 0; return; } BundleLoadInfo info = requestList[0]; loadIndex++; state = BundleLoadState.InProgress; if (ZBundleManager.Instance.HasBundle(info.path)) { if (info.completeFun != null) { info.completeFun.Invoke(info.path, true); } RemoveLoadInfo(info.path); LoadNext(); } else { if (info.IsWeb) { StartCoroutine(LoadRemoteInspector(info)); } else { StartCoroutine(LoadLocalInspector(info)); } } }
public void LoadBundles(BundleInfo[] list) { if (state == BundleLoadState.InProgress || list == null) { return; } ZLog.Log("LoadBundles ..................num = " + list.Length); state = BundleLoadState.Failure; loadIndex = 0; requestList.Clear(); for (int i = 0; i < list.Length; i++) { BundleInfo model = list[i]; if (!HasLoadInfo(model.path)) { BundleLoadInfo info = new BundleLoadInfo(model.path, model.type) { id = model.id, downURL = model.url }; requestList.Add(info); } } totalLength = requestList.Count; LoadNext(); }
private void RemoveLoadInfo(string address) { for (int i = 0; i < requestList.Count; i++) { BundleLoadInfo mode = requestList[i]; if (mode.path == address) { requestList.RemoveAt(i); break; } } }
private bool HasLoadInfo(string address) { if (string.IsNullOrEmpty(address)) { return(false); } for (int i = 0; i < requestList.Count; i++) { BundleLoadInfo mode = requestList[i]; if (mode.path == address) { return(true); } } return(false); }
public void Load(string path, BundleType type, UnityAction <string, bool> complete = null) { if (string.IsNullOrEmpty(path) || !File.Exists(path)) { return; } if (!HasLoadInfo(path)) { BundleLoadInfo info = new BundleLoadInfo(path, type) { completeFun = complete }; requestList.Add(info); } totalLength = requestList.Count; if (state != BundleLoadState.InProgress) { LoadNext(); } }
IEnumerator LoadLocalInspector(BundleLoadInfo info) { AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(info.path); yield return(request); if (request.isDone) { AssetBundle bundle = request.assetBundle; if (bundle != null) { ZBundleManager.Instance.AddBundle(info.path, info.type, bundle); } if (info.completeFun != null) { info.completeFun.Invoke(info.path, true); } } RemoveLoadInfo(info.path); yield return(null); LoadNext(); }
IEnumerator LoadRemoteInspector(BundleLoadInfo info) { string dir = Path.GetDirectoryName(info.path); if (Directory.Exists(dir) == false) { Directory.CreateDirectory(dir); } if (File.Exists(info.path)) { File.Delete(info.path); } yield return(null); UnityWebRequest downloader = UnityWebRequest.Get(info.downURL); downloader.disposeDownloadHandlerOnDispose = true; downloader.downloadHandler = new DownloadHandlerFile(info.path); downloader.SendWebRequest(); while (!downloader.isDone) { yield return(null); } if (!string.IsNullOrEmpty(downloader.error) || downloader.isNetworkError || downloader.isHttpError) { ZLog.Warning("download asset bundle file failed!!!!" + downloader.error + "; url = " + info.downURL); if (info.completeFun != null) { info.completeFun.Invoke(info.downURL, false); } downloader.Dispose(); downloader = null; } else { ZLog.Warning("download asset bundle success that url = " + info.downURL); yield return(null); downloader.Abort(); downloader.Dispose(); downloader = null; Resources.UnloadUnusedAssets(); yield return(null); if (info.completeFun != null) { info.completeFun.Invoke(info.downURL, true); } AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(info.path); yield return(request); if (request.isDone) { AssetBundle bundle = request.assetBundle; ZLog.Warning("read asset bundle success that path = " + info.path); if (bundle != null) { ZBundleManager.Instance.AddBundle(info.path, info.type, bundle); } if (info.completeFun != null) { info.completeFun.Invoke(info.path, true); } } } RemoveLoadInfo(info.path); LoadNext(); }