//load multi bundles,this should be called when load scene. public void bulkLoad(string[] bundleNames, OnBundleLoaded onComplete) { loadingBundles.Clear(); for (int i = bundleNames.Length - 1; i > -1; i--) { string bundleName = bundleNames[i]; Bundle bundle = downloadedBundles[bundleName]; if (!loadedBundles.ContainsKey(bundleName)) { string url = baseURL + bundle.name; bundle.www = WWW.LoadFromCacheOrDownload(url, bundle.version); loadingBundles.Add(bundle); } } this.loading = loadingBundles.Count > 0; if (this.loading) { this.enabled = true; this.onBundleLoaded = onComplete; } else { onComplete(); } }
//dynamically load a bundle. public void load(string bundleName, OnBundleLoaded onComplete) { if (loadedBundles.ContainsKey(bundleName)) { onComplete(); } else { if (loading) { if (pendings.ContainsKey(bundleName)) { pendings[bundleName].Add(onComplete); } else { List <OnBundleLoaded> listener = new List <OnBundleLoaded>(); pendings[bundleName] = listener; listener.Add(onComplete); } checkPending = true; } else { bulkLoad(new string[] { bundleName }, onComplete); } } }
public void Update() { if (loading) { //loading into memory process for (int i = loadingBundles.Count - 1; i > -1; i--) { Bundle bundle = loadingBundles[i]; if (bundle.www.isDone) { loadedBundles[bundle.name] = bundle; loadingBundles.RemoveAt(i); if (loadingScene) { bundle.sceneBundle = bundle.www.assetBundle; } } } loading = loadingBundles.Count > 0; if (!loading) { this.enabled = checkPending; if (this.loadingScene) { this.loadingScene = false; OnSceneLoaded callback = this.onSceneLoaded; this.onSceneLoaded = null; if (callback != null) { callback(); } } else { OnBundleLoaded callback = this.onBundleLoaded; this.onBundleLoaded = null; if (callback != null) { callback(); } } if (needNotifyPending) { notifyPending(); } } } else if (checkPending) { checkPending = false; loadPending(); } else { //downloading process if (pendingBundles == null) { return; } if (!downloading) { for (int i = pendingBundles.Count - 1; i > -1; i--) { Bundle bundle = pendingBundles[i]; if (bundle.completed) { pendingBundles.RemoveAt(i); completedCount++; } else if (!bundle.started) { currentBundle = bundle; currentBundle.started = true; downloading = true; StartCoroutine(downloadBundle(currentBundle)); break; } } } completed = pendingBundles.Count == 0; //notify progress listener if any if (progressListener != null) { //current single bundle download progress float currentBundleProgress = 0; if (currentBundle != null && currentBundle.www != null) { currentBundleProgress = currentBundle.www.progress; } else if (completed) { currentBundleProgress = 1f; totalCount = completedCount; } progressListener.onProgress(totalCount, completedCount, currentBundleProgress); if (completed) { IProgressListener listener = progressListener; progressListener = null; StartCoroutine(notifyCompleted(listener)); } } } }