/// <summary> /// get last cached manifest, to support offline play /// </summary> /// <returns></returns> public static bool TryGetCachedManifest(out AssetbundleBuildManifest manifest) { var path = Path.Combine(Application.persistentDataPath, CACHED_MANIFEST_FILE_NAME); var cachedManifestJson = string.Empty; if (File.Exists(path)) { cachedManifestJson = File.ReadAllText(path); } return(AssetbundleBuildManifest.TryParse(cachedManifestJson, out manifest)); }
static IEnumerator CoGetManifest(BundleAsyncOperation <AssetbundleBuildManifest> result) { if (!Initialized) { Debug.LogError("Do Initialize first"); result.Done(BundleErrorCode.NotInitialized); yield break; } #if UNITY_EDITOR if (UseAssetDatabase) { result.Result = new AssetbundleBuildManifest(); result.Done(BundleErrorCode.Success); yield break; } #endif var manifestReq = UnityWebRequest.Get(Utility.CombinePath(RemoteURL, AssetbundleBuildSettings.ManifestFileName).Replace('\\', '/')); yield return(manifestReq.SendWebRequest()); if (result.IsCancelled) { manifestReq.Dispose(); yield break; } if (manifestReq.isHttpError || manifestReq.isNetworkError) { result.Done(BundleErrorCode.NetworkError); yield break; } var remoteManifestJson = manifestReq.downloadHandler.text; manifestReq.Dispose(); if (!AssetbundleBuildManifest.TryParse(remoteManifestJson, out var remoteManifest)) { result.Done(BundleErrorCode.ManifestParseError); yield break; } result.Result = remoteManifest; result.Done(BundleErrorCode.Success); }
/// <summary> /// get last cached manifest, to support offline play /// </summary> /// <returns></returns> public static bool TryGetCachedManifest(out AssetbundleBuildManifest manifest) { return(AssetbundleBuildManifest.TryParse(PlayerPrefs.GetString("CachedManifest", string.Empty), out manifest)); }
static IEnumerator CoInitalizeLocalBundles(BundleAsyncOperation result, bool autoReloadBundle) { if (Initialized) { result.Done(BundleErrorCode.Success); yield break; } AutoReloadBundle = autoReloadBundle; if (LogMessages) { Debug.Log($"LocalURL : {LocalURL}"); } foreach (var kv in s_AssetBundles) { kv.Value.Bundle.Unload(false); } s_SceneNames.Clear(); s_AssetBundles.Clear(); s_LocalBundles.Clear(); var manifestReq = UnityWebRequest.Get(Utility.CombinePath(LocalURL, AssetbundleBuildSettings.ManifestFileName)); yield return(manifestReq.SendWebRequest()); if (manifestReq.isHttpError || manifestReq.isNetworkError) { result.Done(BundleErrorCode.NetworkError); yield break; } if (!AssetbundleBuildManifest.TryParse(manifestReq.downloadHandler.text, out var localManifest)) { result.Done(BundleErrorCode.ManifestParseError); yield break; } //cached version is recent one. var cacheIsValid = AssetbundleBuildManifest.TryParse(PlayerPrefs.GetString("CachedManifest", string.Empty), out var cachedManifest) && cachedManifest.BuildTime > localManifest.BuildTime; result.SetIndexLength(localManifest.BundleInfos.Count); for (int i = 0; i < localManifest.BundleInfos.Count; i++) { result.SetCurrentIndex(i); result.SetCachedBundle(true); AssetbundleBuildManifest.BundleInfo bundleInfoToLoad; AssetbundleBuildManifest.BundleInfo cachedBundleInfo = default; var localBundleInfo = localManifest.BundleInfos[i]; bool useLocalBundle = !cacheIsValid || //cache is not valid or... !cachedManifest.TryGetBundleInfo(localBundleInfo.BundleName, out cachedBundleInfo) || //missing bundle or... !Caching.IsVersionCached(cachedBundleInfo.AsCached); //is not cached no unusable. bundleInfoToLoad = useLocalBundle ? localBundleInfo : cachedBundleInfo; var loadPath = Utility.CombinePath(LocalURL, bundleInfoToLoad.BundleName); var bundleReq = UnityWebRequestAssetBundle.GetAssetBundle(loadPath, bundleInfoToLoad.Hash); var bundleOp = bundleReq.SendWebRequest(); while (!bundleOp.isDone) { result.SetProgress(bundleOp.progress); yield return(null); } if (!bundleReq.isHttpError && !bundleReq.isNetworkError) { var loadedBundle = new LoadedBundle(bundleInfoToLoad, loadPath, DownloadHandlerAssetBundle.GetContent(bundleReq), useLocalBundle); s_AssetBundles.Add(localBundleInfo.BundleName, loadedBundle); CollectSceneNames(loadedBundle); if (LogMessages) { Debug.Log($"Local bundle Loaded - Name : {localBundleInfo.BundleName}, Hash : {bundleInfoToLoad.Hash }"); } } else { result.Done(BundleErrorCode.NetworkError); yield break; } bundleReq.Dispose(); s_LocalBundles.Add(localBundleInfo.BundleName, localBundleInfo.Hash); } RemoteURL = Utility.CombinePath(localManifest.RemoteURL, localManifest.BuildTarget); #if UNITY_EDITOR if (s_EditorBuildSettings.EmulateWithoutRemoteURL) { RemoteURL = "file://" + Utility.CombinePath(s_EditorBuildSettings.RemoteOutputPath, UnityEditor.EditorUserBuildSettings.activeBuildTarget.ToString()); } #endif Initialized = true; if (LogMessages) { Debug.Log($"Initialize Success \nRemote URL : {RemoteURL} \nLocal URL : {LocalURL}"); } result.Done(BundleErrorCode.Success); }