private void Awake() { // 创建GameObject对象 GameObject gameObj = GameObject.Find("Image"); image = gameObj.GetComponent <Image>(); if (image != null) { Debug.Log("@@@@@@@@@@@@@"); } System.Action <Object> calls = Call_back; AssetBundleManager.Initialization(); AssetBundleManager mgr = AssetBundleManager.GetSingel(); mgr.itfDownloadPath = Application.persistentDataPath + "/"; mgr.InitManifest(); /* * mgr.LoadAssetAsyc("Assets/StreamingAssets/assetbundles/", "bundleinfo", calls);*/ //UnityEngine.Object pic = mgr._LoadSingleAssetInternal("Assets/StreamingAssets/assetbundles/pic/","2.jpg"); //AB下载测试 AssetBundleInfoManager.Initialization(); AssetBundleInfoManager info = AssetBundleInfoManager.GetSingel(); AssetBundleDownloader.Initialization(); AssetBundleDownloader downloader = AssetBundleDownloader.GetSingel(); downloader.SetSourceAssetBundleURL("http://192.168.101.57/F%3A/filesserver/assetbundles/"); System.Action <bool> call = update_call; downloader.CheckUpdate(call); }
private void Awake() { this.assetBundleDownloader = new AssetBundleDownloader(); this.assetLoader = new AssetLoader(); this.assetBundleInfoManager = new AssetBundleInfoManager(); this.onMemoryAssetBundleManager = new OnMemoryAssetBundleManager(); }
/// <summary> /// 更新AssetBundle /// </summary> IEnumerator StartUpdateAssetBundle() { Debug.Log("AssetUpdater:StartUpdateAssetBundle"); if (ErrorCode != EmErrorCode.None) { yield break; } UpdateCompleteValue(0f, 0f); ////载入MainManifest AssetBundleManifest manifest = ZTAssetBundleManager.GetInstance().MainManifest; //载入新的ResourcesManifest string file = DownLoadCommon.GetCacheFileFullName(DownLoadCommon.MAIN_MANIFEST_FILE_NAME); AssetBundleManifest new_manifest = DownLoadCommon.LoadMainManifestByPath(file); if (new_manifest == null) { Error(EmErrorCode.LoadNewMainManifestFailed , "Can't find new version MainManifest!"); yield break; } ////获取需下载的资源列表与删除的资源的列表 List <string> download_files = new List <string>(); List <string> delete_files = new List <string>(); CompareAssetBundleDifference(ref download_files, ref delete_files , manifest, new_manifest); //删除已废弃的文件 if (delete_files.Count > 0) { for (int i = 0; i < delete_files.Count; ++i) { string full_name = DownLoadCommon.GetFileFullName(delete_files[i]); if (File.Exists(full_name)) { File.Delete(full_name); yield return(0); } } } //更新所有需下载的资源 _ab_download = new AssetBundleDownloader(_current_ab_url); _ab_download.Start(DownLoadCommon.PATH, download_files); while (!_ab_download.IsDone) { UpdateCompleteValue(_ab_download.CompletedSize, _ab_download.TotalSize); yield return(0); } if (_ab_download.IsFailed) { Error(EmErrorCode.DownloadAssetBundleFailed); yield break; } }
/// <summary> /// /// </summary> public void AbortUpdate() { StopAllCoroutines(); if (_verifier != null) { _verifier.Abort(); _verifier = null; } if (_file_download != null) { _file_download.Abort(); _file_download = null; } if (_ab_download != null) { _ab_download.Abort(); _ab_download = null; } SaveDownloadCacheData(); UpdateState(EmState.Abort); Done(); }
protected virtual IAssetLoader <GameObject> InitializeAssetLoader() { var projectName = SpatialOS.Configuration.ProjectName; var deployment = SpatialOS.Deployment; var assemblyName = deployment.HasValue ? deployment.Value.AssemblyName : SpatialOS.Configuration.AssemblyName; // If an assembly name is set, default to streaming from it. The strategy can still be overridden by the command line. AssetDatabaseStrategy defaultStrategy = AssetDatabaseStrategy.Local; if (!string.IsNullOrEmpty(projectName) && !string.IsNullOrEmpty(assemblyName)) { defaultStrategy = AssetDatabaseStrategy.Streaming; } UseLocalPrefabs = SpatialOS.Configuration.GetCommandLineValue(CommandLineConfigNames.UseLocalPrefabs, UseLocalPrefabs); LoadingStrategy = SpatialOS.Configuration.GetCommandLineValue(CommandLineConfigNames.AssetDatabaseStrategy, defaultStrategy); LocalAssetDatabasePath = SpatialOS.Configuration.GetCommandLineValue(CommandLineConfigNames.LocalAssetDatabasePath, LocalAssetDatabasePath); IAssetLoader <GameObject> gameObjectLoader; if (Application.isEditor && UseLocalPrefabs) { gameObjectLoader = new PrefabGameObjectLoader(); } else { switch (LoadingStrategy) { case AssetDatabaseStrategy.Local: var path = Path.GetFullPath(LocalAssetDatabasePath); gameObjectLoader = new GameObjectFromAssetBundleLoader(new LocalAssetBundleLoader(path)); break; case AssetDatabaseStrategy.Streaming: pendingPrepareTemplates = new List <Action>(); var cachePath = Path.Combine(Application.persistentDataPath, "cache" + WorkerTypeUtils.ToWorkerName(SpatialOS.Configuration.WorkerPlatform)); Directory.CreateDirectory(cachePath); var assetBundleDownloader = new AssetBundleDownloader(cachePath, new MachineCache <byte[], AssetBundle>(Path.Combine(cachePath, "assets"), new AssetBundlePersistenceStrategy()), new MachineCache <CacheEntry, CacheEntry>(Path.Combine(cachePath, "asset-metadata"), new AssetMetadataPersistenceStrategy()), new WWWRequest(), this); assetBundleDownloader.GetAssetUrl = GetAssetUrl; var exponentialBackoffRetryAssetLoader = gameObject.GetComponent <ExponentialBackoffRetryAssetLoader>() ?? gameObject.AddComponent <ExponentialBackoffRetryAssetLoader>(); exponentialBackoffRetryAssetLoader.Init(assetBundleDownloader, SpatialOS.Configuration.GetCommandLineValue(CommandLineConfigNames.MaxAssetLoadingRetries, -1), SpatialOS.Configuration.GetCommandLineValue(CommandLineConfigNames.AssetLoadingRetryBackoffMilliseconds, -1)); gameObjectLoader = new GameObjectFromAssetBundleLoader(exponentialBackoffRetryAssetLoader); pendingUrlResolveRequest = CloudAssemblyArtifactResolver.ResolveAssetUrls(this, new WWWRequest(), SpatialOS.Configuration.InfraServiceUrl, projectName, assemblyName, OnAssetBundleNameToUrlMapResolved, OnAssetResolveFailed); break; default: throw new Exception(string.Format("Unknown loading strategy '{0}'", LoadingStrategy)); } } return(gameObjectLoader); }
/** * load resource from Unity's AssetBundle cache. */ public static IEnumerator LoadCachedBundle <T> ( string bundleName, string resourceName, uint crc, System.Action <string, T> succeeded, System.Action <string, string> failed) where T : UnityEngine.Object { Debug.Log("start LoadCachedBundle for Resource:" + resourceName + " from bundleName:" + bundleName); /* * wait if marked as loading. */ while (cachedAssetBundleLoadingList.Contains(bundleName)) { Debug.Log("belonging assetBundle is loading. assetBundleName:" + bundleName + " resourceName:" + resourceName); yield return(null); } /* * load if already allocated to onMemoryCache = onMemoryAssetBundleDict. */ if (onMemoryAssetBundleDict.ContainsKey(bundleName)) { var assetBundle1 = onMemoryAssetBundleDict[bundleName]; var loadedResource1 = (T)assetBundle1.Load(resourceName, typeof(T)); if (loadedResource1 == null) { failed(resourceName, "resouce-is-null-in-assetBundle-1:" + bundleName); yield break; } Debug.Log("load from cache is done, resourceName:" + resourceName); succeeded(resourceName, loadedResource1); yield break; } // not on memory yet, start loading from AssetBundle cache. string url = AssetBundleDownloader.GetFileProtocolUrlForAssetBundle(bundleName); var isCached = Caching.IsVersionCached(url, AssetBundleDownloader.FIXED_CACHED_SLOT_VERSION); // use fixed version parameter. it's not version, this is SLOT. if (!isCached) { failed(resourceName, "assetBundle-not-cached:" + url + " maybe expired."); yield break; } // start loading from Unity's AssetBundle cache. set loading mark. cachedAssetBundleLoadingList.Add(bundleName); using (var www = WWW.LoadFromCacheOrDownload(url, AssetBundleDownloader.FIXED_CACHED_SLOT_VERSION, crc)) { // use fixed version parameter. it's not version, this is SLOT. yield return(www); if (!String.IsNullOrEmpty(www.error)) { if (cachedAssetBundleLoadingList.Contains(bundleName)) { cachedAssetBundleLoadingList.Remove(bundleName); } failed(resourceName, "www-error:" + www.error); yield break; } // remove loading mark. if (cachedAssetBundleLoadingList.Contains(bundleName)) { cachedAssetBundleLoadingList.Remove(bundleName); } var assetBundle2 = www.assetBundle; // set to on-memory cache onMemoryAssetBundleDict[bundleName] = assetBundle2; } var loadedResource2 = (T)onMemoryAssetBundleDict[bundleName].Load(resourceName, typeof(T)); if (loadedResource2 == null) { if (cachedAssetBundleLoadingList.Contains(bundleName)) { cachedAssetBundleLoadingList.Remove(bundleName); } failed(resourceName, "resouce-is-null-in-assetBundle-2:" + bundleName); yield break; } succeeded(resourceName, loadedResource2); }
/// <summary> /// 启动网络资源更新 /// /// 步骤: /// 1 将打包工具打包好的资源放在服务器端 需要更新的内容是一个配置文件服务器端读取 /// 2 客户端向服务器端请求要更新的内容和更新服务器的地址 /// 3 调用此函数进行更新 /// /// </summary> /// <param name="rString">需要更新的内容 -- 由打包工具生成 然后放到服务器端</param> /// <param name="sString">服务器资源更新地址 -- 也是由服务器端下发</param> public void StartUpdateAsset(string rString, string sString) { AssetBundleDownloader abDownloader = new AssetBundleDownloader(rString, sString); StartCoroutine(abDownloader.StartDownload()); }