public AssetBundleManager(string bundleDownladerUri, AssetPath assetPath = AssetPath.streamingAssetsPath) { switch (assetPath) { case AssetPath.streamingAssetsPath: uriString = BundleUtil.GetReadOnlyDirectory(); break; case AssetPath.persistentDataPath: uriString = BundleUtil.GetStorableDirectory(); break; case AssetPath.temporaryCachePath: uriString = BundleUtil.GetTemporaryCacheDirectory(); break; } this.resources = null; if (string.IsNullOrEmpty(bundleDownladerUri)) { return; } Uri baseUri = new Uri(bundleDownladerUri); this.downloader = new WWWDownloader(baseUri, false); }
public IEnumerator Download(List <string> bundleNames) { this.downloading = true; try { IProgressResult <Progress, BundleManifest> manifestResult = this.downloader.DownloadManifest(BundleSetting.ManifestFilename); yield return(manifestResult.WaitForDone()); if (manifestResult.Exception != null) { Debug.LogFormat("Downloads BundleManifest failure.Error:{0}", manifestResult.Exception); yield break; } BundleManifest manifest = manifestResult.Result; IProgressResult <float, List <BundleInfo> > bundlesResult = this.downloader.GetDownloadList(manifest); yield return(bundlesResult.WaitForDone()); List <BundleInfo> bundles = bundlesResult.Result.FindAll(obj => bundleNames.Contains(obj.FullName)); if (bundles == null || bundles.Count <= 0) { Debug.LogFormat("Please clear cache and remove StreamingAssets,try again."); yield break; } IProgressResult <Progress, bool> downloadResult = this.downloader.DownloadBundles(bundles); downloadResult.Callbackable().OnProgressCallback(p => { Debug.LogFormat("Downloading {0:F2}KB/{1:F2}KB {2:F3}KB/S", p.GetCompletedSize(UNIT.KB), p.GetTotalSize(UNIT.KB), p.GetSpeed(UNIT.KB)); }); yield return(downloadResult.WaitForDone()); if (downloadResult.Exception != null) { Debug.LogFormat("Downloads AssetBundle failure.Error:{0}", downloadResult.Exception); yield break; } if (this.resources != null) { //update BundleManager's manifest BundleManager manager = (this.resources as BundleResources).BundleManager as BundleManager; manager.BundleManifest = manifest; } #if UNITY_EDITOR UnityEditor.EditorUtility.OpenWithDefaultApp(BundleUtil.GetReadOnlyDirectory()); #endif } finally { this.downloading = false; } }
IResources GetResources() { if (this.resources != null) { return(this.resources); } /* Create a BundleManifestLoader. */ IBundleManifestLoader manifestLoader = new BundleManifestLoader(); /* Loads BundleManifest. */ BundleManifest manifest = manifestLoader.Load(BundleUtil.GetStorableDirectory() + BundleSetting.ManifestFilename); //manifest.ActiveVariants = new string[] { "", "sd" }; //manifest.ActiveVariants = new string[] { "", "hd" }; /* Create a PathInfoParser. */ IPathInfoParser pathInfoParser = new AutoMappingPathInfoParser(manifest); /* Use a custom BundleLoaderBuilder */ ILoaderBuilder builder = new CustomBundleLoaderBuilder(new Uri(BundleUtil.GetReadOnlyDirectory()), false); /* Create a BundleManager */ IBundleManager manager = new BundleManager(manifest, builder); /* Create a BundleResources */ this.resources = new BundleResources(pathInfoParser, manager); return(this.resources); }
IEnumerator _CopyManifestFromStreamingAssets(Action callback) { DirectoryInfo dir = new DirectoryInfo(BundleUtil.GetReadOnlyDirectory() + "/"); Uri baseUri = new Uri(dir.FullName); IDownloader downloader = new WWWDownloader(baseUri, false); // 下载 Manifest IProgressResult <Progress, BundleManifest> manifestResult = downloader.DownloadManifest(BundleSetting.ManifestFilename); yield return(manifestResult.WaitForDone()); if (manifestResult.Exception != null) { LogManager.Log("Downloads BundleManifest failure.Error:{0}", manifestResult.Exception); yield break; } IResources _resources = CreateResources(); context.GetContainer().Register <IResources>(_resources); if (callback != null) { callback.Invoke(); } }
public override BundleLoader Create(BundleManager manager, BundleInfo bundleInfo, BundleLoader[] dependencies) { //Customize the rules for finding assets. Uri loadBaseUri = this.BaseUri; //eg: http://your ip/bundles if (this.useCache && BundleUtil.ExistsInCache(bundleInfo)) { //Load assets from the cache of Unity3d. loadBaseUri = this.BaseUri; #if UNITY_5_4_OR_NEWER return(new UnityWebRequestBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, dependencies, manager, this.useCache)); #else return(new WWWBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, dependencies, manager, this.useCache)); #endif } if (BundleUtil.ExistsInStorableDirectory(bundleInfo)) { //Load assets from the "Application.persistentDataPath/bundles" folder. /* Path: Application.persistentDataPath + "/bundles/" + bundleInfo.Filename */ loadBaseUri = new Uri(BundleUtil.GetStorableDirectory()); } #if !UNITY_WEBGL || UNITY_EDITOR else if (BundleUtil.ExistsInReadOnlyDirectory(bundleInfo)) { //Load assets from the "Application.streamingAssetsPath/bundles" folder. /* Path: Application.streamingAssetsPath + "/bundles/" + bundleInfo.Filename */ loadBaseUri = new Uri(BundleUtil.GetReadOnlyDirectory()); } #endif if (bundleInfo.IsEncrypted) { if (this.decryptor != null && bundleInfo.Encoding.Equals(decryptor.AlgorithmName)) { return(new CryptographBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, dependencies, manager, decryptor)); } throw new NotSupportedException(string.Format("Not support the encryption algorithm '{0}'.", bundleInfo.Encoding)); } //Loads assets from an Internet address if it does not exist in the local directory. #if UNITY_5_4_OR_NEWER if (this.IsRemoteUri(loadBaseUri)) { return(new UnityWebRequestBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, dependencies, manager, this.useCache)); } else { return(new FileAsyncBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, dependencies, manager)); } #else return(new WWWBundleLoader(new Uri(loadBaseUri, bundleInfo.Filename), bundleInfo, dependencies, manager, this.useCache)); #endif }
IResources CreateResources() { IResources resources = null; #if UNITY_EDITOR if (SimulationSetting.IsSimulationMode) { Debug.Log("Use SimulationResources. Run In Editor"); /* Create a PathInfoParser. */ //IPathInfoParser pathInfoParser = new SimplePathInfoParser("@"); IPathInfoParser pathInfoParser = new SimulationAutoMappingPathInfoParser(); /* Create a BundleManager */ IBundleManager manager = new SimulationBundleManager(); /* Create a BundleResources */ resources = new SimulationResources(pathInfoParser, manager); } else #endif { /* Create a BundleManifestLoader. */ IBundleManifestLoader manifestLoader = new BundleManifestLoader(); /* Loads BundleManifest. */ BundleManifest manifest = manifestLoader.Load(BundleUtil.GetReadOnlyDirectory() + BundleSetting.ManifestFilename); //manifest.ActiveVariants = new string[] { "", "sd" }; manifest.ActiveVariants = new string[] { "", "hd" }; /* Create a PathInfoParser. */ //IPathInfoParser pathInfoParser = new SimplePathInfoParser("@"); IPathInfoParser pathInfoParser = new AutoMappingPathInfoParser(manifest); /* Create a BundleLoaderBuilder */ //ILoaderBuilder builder = new WWWBundleLoaderBuilder(new Uri(BundleUtil.GetReadOnlyDirectory()), false); /* AES128_CBC_PKCS7 */ //RijndaelCryptograph rijndaelCryptograph = new RijndaelCryptograph(128, Encoding.ASCII.GetBytes(this.key), Encoding.ASCII.GetBytes(this.iv)); IStreamDecryptor decryptor = CryptographUtil.GetDecryptor(Algorithm.AES128_CBC_PKCS7, Encoding.ASCII.GetBytes(this.key), Encoding.ASCII.GetBytes(this.iv)); /* Use a custom BundleLoaderBuilder */ ILoaderBuilder builder = new CustomBundleLoaderBuilder(new Uri(BundleUtil.GetReadOnlyDirectory()), false, decryptor); /* Create a BundleManager */ IBundleManager manager = new BundleManager(manifest, builder); /* Create a BundleResources */ resources = new BundleResources(pathInfoParser, manager); } return(resources); }
void OnGUI() { if (!downloading) { GUILayout.Space(20); GUILayout.BeginHorizontal(); GUILayout.Space(20); GUILayout.BeginVertical(); if (GUILayout.Button("Clear persistentDataPath")) { #if UNITY_2017_1_OR_NEWER Caching.ClearCache(); #else Caching.CleanCache(); #endif BundleUtil.ClearStorableDirectory(); } #if UNITY_EDITOR if (GUILayout.Button("Remove StreamingAssets")) { if (Directory.Exists(BundleUtil.GetReadOnlyDirectory())) { Directory.Delete(BundleUtil.GetReadOnlyDirectory(), true); } UnityEditor.AssetDatabase.Refresh(); } #endif GUILayout.Space(5); if (GUILayout.Button("Download AssetBundle")) { StartCoroutine(Download()); } if (GUILayout.Button("Load an asset")) { if (!File.Exists(BundleUtil.GetStorableDirectory() + BundleSetting.ManifestFilename)) { Debug.LogFormat("Please download assetbundles first,try again."); } else { this.LoadAsset("LoxodonFramework/BundleExamples/Models/Red/Red.prefab"); } } GUILayout.EndVertical(); GUILayout.EndHorizontal(); } }
public virtual void ClearFromStreamingAssets() { try { AssetDatabase.StartAssetEditing(); DirectoryInfo dir = new DirectoryInfo(BundleUtil.GetReadOnlyDirectory()); if (dir.Exists) { dir.Delete(true); } AssetDatabase.Refresh(); } finally { AssetDatabase.StopAssetEditing(); } }
void Start() { #if UNITY_WEBGL && !UNITY_EDITOR Uri baseUri = new Uri(BundleUtil.GetReadOnlyDirectory()); #else DirectoryInfo dir = new DirectoryInfo("./AssetBundles/StandaloneWindows/1.0.0/"); if (!dir.Exists) { Debug.LogFormat("Directory '{0}' does not exist.", dir.FullName); return; } Uri baseUri = new Uri(dir.FullName); #endif this.downloader = new WWWDownloader(baseUri, false); }
IResources CreateResources(BundleManifest manifest) { IResources resources = null; #if UNITY_EDITOR if (SimulationSetting.IsSimulationMode) { Debug.Log("Use SimulationResources. Run In Editor"); /* Create a PathInfoParser. */ //IPathInfoParser pathInfoParser = new SimplePathInfoParser("@"); IPathInfoParser pathInfoParser = new SimulationAutoMappingPathInfoParser(); /* Create a BundleManager */ IBundleManager manager = new SimulationBundleManager(); /* Create a BundleResources */ resources = new SimulationResources(pathInfoParser, manager); } else #endif { /* Create a PathInfoParser. */ //IPathInfoParser pathInfoParser = new SimplePathInfoParser("@"); IPathInfoParser pathInfoParser = new AutoMappingPathInfoParser(manifest); /* Create a BundleLoaderBuilder */ //ILoaderBuilder builder = new WWWBundleLoaderBuilder(new Uri(BundleUtil.GetReadOnlyDirectory()), false); /* AES128_CBC_PKCS7 */ RijndaelCryptograph rijndaelCryptograph = new RijndaelCryptograph(128, Encoding.ASCII.GetBytes(this.key), Encoding.ASCII.GetBytes(this.iv)); /* Use a custom BundleLoaderBuilder */ ILoaderBuilder builder = new CustomBundleLoaderBuilder(new Uri(BundleUtil.GetReadOnlyDirectory()), false, rijndaelCryptograph); /* Create a BundleManager */ IBundleManager manager = new BundleManager(manifest, builder); /* Create a BundleResources */ resources = new BundleResources(pathInfoParser, manager); } return(resources); }
IEnumerator Start() { ApplicationContext context = Context.GetApplicationContext(); /* Create a BundleManifestLoader. */ IBundleManifestLoader manifestLoader = new BundleManifestLoader(); /* Loads BundleManifest. */ IAsyncResult <BundleManifest> result = manifestLoader.LoadAsync(BundleUtil.GetReadOnlyDirectory() + BundleSetting.ManifestFilename); yield return(result.WaitForDone()); BundleManifest manifest = result.Result; //manifest.ActiveVariants = new string[] { "", "sd" }; manifest.ActiveVariants = new string[] { "", "hd" }; this.resources = CreateResources(manifest); context.GetContainer().Register <IResources>(this.resources); }
void Awake() { /* Create a BundleManifestLoader. */ IBundleManifestLoader manifestLoader = new BundleManifestLoader(); /* Loads BundleManifest. */ BundleManifest manifest = manifestLoader.Load(BundleUtil.GetReadOnlyDirectory() + BundleSetting.ManifestFilename); /* Create a PathInfoParser. */ IPathInfoParser pathInfoParser = new AutoMappingPathInfoParser(manifest); /* Use a BundleLoaderBuilder */ ILoaderBuilder builder = new CustomBundleLoaderBuilder(new Uri(BundleUtil.GetReadOnlyDirectory()), false, new RijndaelCryptograph(128, Encoding.ASCII.GetBytes(key), Encoding.ASCII.GetBytes(iv))); /* Create a BundleManager */ IBundleManager manager = new BundleManager(manifest, builder); /* Create a BundleResources */ resources = new BundleResources(pathInfoParser, manager); }
void Awake() { /* Create a BundleManifestLoader. */ IBundleManifestLoader manifestLoader = new BundleManifestLoader(); /* Loads BundleManifest. */ BundleManifest manifest = manifestLoader.Load(BundleUtil.GetReadOnlyDirectory() + BundleSetting.ManifestFilename); /* Create a PathInfoParser. */ IPathInfoParser pathInfoParser = new AutoMappingPathInfoParser(manifest); IStreamDecryptor decryptor = CryptographUtil.GetDecryptor(Algorithm.AES128_CBC_PKCS7, Encoding.ASCII.GetBytes(this.key), Encoding.ASCII.GetBytes(this.iv)); /* Use a BundleLoaderBuilder */ ILoaderBuilder builder = new CustomBundleLoaderBuilder(new Uri(BundleUtil.GetReadOnlyDirectory()), false, decryptor); /* Create a BundleManager */ IBundleManager manager = new BundleManager(manifest, builder); /* Create a BundleResources */ resources = new BundleResources(pathInfoParser, manager); }
void Start() { #if UNITY_WEBGL && !UNITY_EDITOR Uri baseUri = new Uri(BundleUtil.GetReadOnlyDirectory()); #elif UNITY_EDITOR DirectoryInfo dir = new DirectoryInfo(string.Format("./AssetBundles/{0}/1.0.0/", UnityEditor.EditorUserBuildSettings.activeBuildTarget)); if (!dir.Exists) { Debug.LogFormat("The '{0}' directory does not exist, please make sure the path is correct and the assetbundle file exists in the directory.", dir.FullName); return; } Uri baseUri = new Uri(dir.FullName); //If you want to test downloading asset bundles from a remote server, please comment the above code, using the code below //Uri baseUri = new Uri("http://your server/platform/bundles/"); #else Uri baseUri = new Uri("http://your server/platform/bundles/"); #endif this.downloader = new WWWDownloader(baseUri, false); }
public virtual void CopyToStreamingAssets() { try { AssetDatabase.StartAssetEditing(); BundleBuilder builder = new BundleBuilder(); DirectoryInfo src = new DirectoryInfo(builder.GetVersionOutput(this.OutputPath, this.BuildTarget, this.DataVersion)); DirectoryInfo dest = new DirectoryInfo(BundleUtil.GetReadOnlyDirectory()); if (dest.Exists) { dest.Delete(true); } if (!dest.Exists) { dest.Create(); } BundleManifest manifest = builder.CopyAssetBundleAndManifest(src, dest); if (manifest != null) { Debug.LogFormat("Copy AssetBundles success."); } AssetDatabase.Refresh(); } catch (Exception e) { Debug.LogFormat("Copy AssetBundles failure. Error:{0}", e); } finally { AssetDatabase.StopAssetEditing(); } }
protected virtual IEnumerator DoDownloadManifest(string relativePath, IProgressPromise <Progress, BundleManifest> promise) { Progress progress = new Progress(); promise.UpdateProgress(progress); byte[] data; string path = this.GetAbsoluteUri(relativePath); #if UNITY_2017_1_OR_NEWER using (UnityWebRequest www = new UnityWebRequest(path)) { www.downloadHandler = new DownloadHandlerBuffer(); #if UNITY_2018_1_OR_NEWER www.SendWebRequest(); #else www.Send(); #endif while (!www.isDone) { if (www.downloadProgress >= 0) { if (progress.TotalSize <= 0) { progress.TotalSize = (long)(www.downloadedBytes / www.downloadProgress); } progress.CompletedSize = (long)www.downloadedBytes; promise.UpdateProgress(progress); } yield return(null); } if (!string.IsNullOrEmpty(www.error)) { promise.SetException(new Exception(www.error)); yield break; } data = www.downloadHandler.data; } #else using (WWW www = new WWW(path)) { while (!www.isDone) { if (www.bytesDownloaded > 0f) { if (progress.TotalSize <= 0) { progress.TotalSize = (long)(www.bytesDownloaded / www.progress); } progress.CompletedSize = www.bytesDownloaded; promise.UpdateProgress(progress); } yield return(null); } progress.CompletedSize = www.bytesDownloaded; promise.UpdateProgress(progress); if (!string.IsNullOrEmpty(www.error)) { promise.SetException(new Exception(www.error)); yield break; } data = www.bytes; } #endif try { BundleManifest manifest = BundleManifest.Parse(Encoding.UTF8.GetString(data)); FileInfo file = new FileInfo(BundleUtil.GetReadOnlyDirectory() + relativePath); if (file.Exists) { FileInfo bakFile = new FileInfo(BundleUtil.GetReadOnlyDirectory() + relativePath + ".bak"); if (bakFile.Exists) { bakFile.Delete(); } file.CopyTo(bakFile.FullName); } if (!file.Directory.Exists) { file.Directory.Create(); } File.WriteAllBytes(file.FullName, data); promise.SetResult(manifest); } catch (IOException e) { promise.SetException(e); } }
protected override IEnumerator DoDownloadBundles(IProgressPromise <Progress, bool> promise, List <BundleInfo> bundles) { long totalSize = 0; long downloadedSize = 0; Progress progress = new Progress(); List <BundleInfo> list = new List <BundleInfo>(); for (int i = 0; i < bundles.Count; i++) { var info = bundles[i]; totalSize += info.FileSize; if (BundleUtil.Exists(info)) { downloadedSize += info.FileSize; continue; } list.Add(info); } progress.TotalSize = totalSize; progress.CompletedSize = downloadedSize; yield return(null); List <KeyValuePair <BundleInfo, UnityWebRequest> > tasks = new List <KeyValuePair <BundleInfo, UnityWebRequest> >(); for (int i = 0; i < list.Count; i++) { BundleInfo bundleInfo = list[i]; UnityWebRequest www; if (useCache && !bundleInfo.IsEncrypted) { #if UNITY_2018_1_OR_NEWER www = UnityWebRequestAssetBundle.GetAssetBundle(GetAbsoluteUri(bundleInfo.Filename), bundleInfo.Hash, 0); #else www = UnityWebRequest.GetAssetBundle(GetAbsoluteUri(bundleInfo.Filename), bundleInfo.Hash, 0); #endif } else { www = new UnityWebRequest(GetAbsoluteUri(bundleInfo.Filename)); www.downloadHandler = new DownloadHandlerBuffer(); } #if UNITY_2018_1_OR_NEWER www.SendWebRequest(); #else www.Send(); #endif tasks.Add(new KeyValuePair <BundleInfo, UnityWebRequest>(bundleInfo, www)); while (tasks.Count >= this.MaxTaskCount || (i == list.Count - 1 && tasks.Count > 0)) { long tmpSize = 0; for (int j = tasks.Count - 1; j >= 0; j--) { var task = tasks[j]; BundleInfo _bundleInfo = task.Key; UnityWebRequest _www = task.Value; if (!_www.isDone) { tmpSize += Math.Max(0, (long)(_www.downloadProgress * _bundleInfo.FileSize)); continue; } tasks.RemoveAt(j); downloadedSize += _bundleInfo.FileSize; if (!string.IsNullOrEmpty(_www.error)) { promise.SetException(new Exception(_www.error)); if (log.IsErrorEnabled) { log.ErrorFormat("Downloads AssetBundle '{0}' failure from the address '{1}'.Reason:{2}", _bundleInfo.FullName, GetAbsoluteUri(_bundleInfo.Filename), _www.error); } yield break; } try { if (useCache && !bundleInfo.IsEncrypted) { AssetBundle bundle = ((DownloadHandlerAssetBundle)_www.downloadHandler).assetBundle; if (bundle != null) { bundle.Unload(true); } } else { string fullname = BundleUtil.GetReadOnlyDirectory() + _bundleInfo.Filename; FileInfo info = new FileInfo(fullname); if (info.Exists) { info.Delete(); } if (!info.Directory.Exists) { info.Directory.Create(); } File.WriteAllBytes(info.FullName, _www.downloadHandler.data); } } catch (Exception e) { promise.SetException(e); if (log.IsErrorEnabled) { log.ErrorFormat("Downloads AssetBundle '{0}' failure from the address '{1}'.Reason:{2}", _bundleInfo.FullName, GetAbsoluteUri(_bundleInfo.Filename), e); } yield break; } } progress.CompletedSize = downloadedSize + tmpSize; promise.UpdateProgress(progress); yield return(null); } } promise.SetResult(true); }