/// <summary> /// Loads asset object from AssetBundles /// </summary> /// <returns>The asset async.</returns> /// <param name="assetBundle">AssetBundle file.</param> /// <param name="onFinish">onFinish callback. Return object was loaded.</param> /// <param name="onFailed">onFailed callback. Return error reason.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public IEnumerator LoadAssetAsync <T>( string assetBundle, Action <T> onFinish, Action <string> onFailed) where T : UnityEngine.Object { if (_assetBundleVersionDB == null) { //Load AssetBundle version first bool isSuccess = false; bool isDownloadVersionFileFinish = false; StartCoroutine(GetAssetBundleVersionFile((fileUrl) => { Debug.Log("Download version file OK " + fileUrl); isSuccess = true; isDownloadVersionFileFinish = true; }, (downloadPath, error) => { isDownloadVersionFileFinish = true; Debug.LogError("Can not download version file from: " + downloadPath); onFailed(error); })); yield return(new WaitUntil(() => { return isDownloadVersionFileFinish; })); if (!isSuccess) { yield break; } } //Check AssetBundle existed in version database or not? AssetBundleSettings.AssetBundleInfo abInfo = _assetBundleVersionDB.lstAssetBundleInfo.Where(_ => _.assetBundle == assetBundle).FirstOrDefault(); if (abInfo == null) { Debug.Log("Not found『" + assetBundle + "』in AssetBundles Version Database"); onFailed("Not found『" + assetBundle + "』in AssetBundles Version Database"); yield break; } //Load from local bool isFinish = false; bool isLoaded = false; StartCoroutine(LoadAssetBundleFromLocalPath <T>(abInfo, (obj) => { isFinish = true; isLoaded = true; onFinish(obj); }, (err) => { isFinish = true; isLoaded = false; onFailed(err); })); yield return(new WaitUntil(() => { return isFinish; })); if (isLoaded) { yield break; } //Can not be loaded from local, so need download from server AssetBundleFileDownload dlObj = new AssetBundleFileDownload(AssetBundleSettings.GetAssetBundleServerURL(), abInfo); DownloadFile(true, dlObj, (www) => { string path = AssetBundleSettings.localFolderPathSaveAB + "/" + abInfo.assetBundle + abInfo.extension; StartCoroutine(ExtractAssetObjectFromAssetBundle(www, abInfo, path, onFinish, onFailed)); dlObj.Dispose(); }, (errMsg) => { onFailed(errMsg); }); }
/// <summary> /// Loads the scene async from AssetBundle /// </summary> /// <returns>The scene async.</returns> /// <param name="assetBundle">AssetBundle file.</param> /// <param name="loadSceneMode">Load scene mode.</param> /// <param name="onFinish">onFinish callback. Return name of scene has just loaded</param> /// <param name="onFailed">onFailed callback Return error reason.</param> public IEnumerator LoadSceneAsync( string assetBundle, UnityEngine.SceneManagement.LoadSceneMode loadSceneMode, Action <string> onFinish, Action <string> onFailed) { bool isSuccess = false; bool isProcessFinish = false; //Load AssetBundle version first if (_assetBundleVersionDB == null) { StartCoroutine(GetAssetBundleVersionFile((fileUrl) => { Debug.Log("Download version file OK " + fileUrl); isSuccess = true; isProcessFinish = true; }, (downloadPath, error) => { isProcessFinish = true; Debug.LogError("Can not download version file from: " + downloadPath); onFailed(error); })); yield return(new WaitUntil(() => { return isProcessFinish; })); if (!isSuccess) { yield break; } } //Check AssetBundle existed in version database or not? AssetBundleSettings.AssetBundleInfo abInfo = _assetBundleVersionDB.lstAssetBundleInfo.Where(_ => _.assetBundle == assetBundle).FirstOrDefault(); if (abInfo == null) { Debug.Log("Not found『" + assetBundle + "』in AssetBundles Version Database"); onFailed("Not found『" + assetBundle + "』in AssetBundles Version Database"); yield break; } //Load from local string path = AssetBundleSettings.localFolderPathSaveAB + "/" + abInfo.assetBundle + abInfo.extension; if (File.Exists(path)) { System.Uri url = new System.Uri(path); WWW _www = new WWW(url.AbsoluteUri); yield return(new WaitUntil(() => { return _www.isDone; })); if (string.IsNullOrEmpty(_www.error)) { isSuccess = false; isProcessFinish = false; StartCoroutine(ExtractSceneNameAndLoadSceneFromAssetBundle(_www, abInfo, path, loadSceneMode, (sceneName) => { isSuccess = true; isProcessFinish = true; onFinish(sceneName); }, (err) => { isSuccess = false; isProcessFinish = true; })); _www.Dispose(); yield return(new WaitUntil(() => { return isProcessFinish; })); if (isSuccess) { yield break; } } else { onFailed("Not Found『" + abInfo.assetBundle + "』In LocalPath: " + path); yield break; } } else { Debug.Log("NOT FOUND " + assetBundle); } //Can not be loaded from local, so need download from server AssetBundleFileDownload dlObj = new AssetBundleFileDownload(AssetBundleSettings.GetAssetBundleServerURL(), abInfo); DownloadFile(true, dlObj, (www) => { string pathLocal = AssetBundleSettings.localFolderPathSaveAB + "/" + abInfo.assetBundle + abInfo.extension; StartCoroutine(ExtractSceneNameAndLoadSceneFromAssetBundle(www, abInfo, pathLocal, loadSceneMode, onFinish, onFailed)); dlObj.Dispose(); }, (errMsg) => { onFailed(errMsg); }); }
/// <summary> /// Downloads AssetBundles from server /// </summary> /// <returns>The assetbundles from server.</returns> /// <param name="onFinish">onFinish callback.</param> /// <param name="onFailed">onFailed callback. Return error reason</param> public IEnumerator DownloadAssetBundlesFromServer(Action onFinish, Action <string> onFailed) { currentDownloadIndex = 0; totalDownloaded = 0; downloadedSize = 0; totalSize = 0; bool isSuccess = false; bool isDownloadVersionFileFinish = false; StartCoroutine(GetAssetBundleVersionFile((fileUrl) => { Debug.Log("Download version file finished: " + fileUrl); isSuccess = true; isDownloadVersionFileFinish = true; }, (downloadPath, error) => { isDownloadVersionFileFinish = true; Debug.LogError("Can not download version file from: " + downloadPath); onFailed(error); })); yield return(new WaitUntil(() => { return isDownloadVersionFileFinish; })); if (!isSuccess) { yield break; } if (_downloadList.Count == 0) { Debug.Log("All AssetBundles were downloaded!"); } bool isError = false; List <AssetBundleFileDownload> _downloadProcessList = new List <AssetBundleFileDownload> (); //Process download file while (true) { if (_downloadProcessList.Count < AssetBundleSettings.DOWNLOAD_NUM_FILE_AT_TIME) { if (currentDownloadIndex <= _downloadList.Count - 1) { AssetBundleFileDownload dlObj = new AssetBundleFileDownload(AssetBundleSettings.GetAssetBundleServerURL(), _downloadList [currentDownloadIndex]); _downloadProcessList.Add(dlObj); currentDownloadIndex++; } if (currentDownloadIndex > _downloadList.Count) { currentDownloadIndex = _downloadList.Count; } } for (int i = 0; i < _downloadProcessList.Count; i++) { AssetBundleFileDownload dlObj = _downloadProcessList[i]; if (!dlObj.isProcessing) { DownloadFile(true, dlObj, (www) => { totalDownloaded++; downloadedSize += dlObj.GetDownloadedSize(); dlObj.Dispose(); _downloadProcessList.Remove(dlObj); }, (errMsg) => { isError = true; if (onFailed != null) { onFailed(errMsg); } }); } } if (isError) { yield break; } if (totalDownloaded == _downloadList.Count) { yield return(null); onFinish(); yield break; } yield return(null); } }