private void DownloadAssetFileCallback(Message <AssetFileDownloadResult> msg) { if (msg.IsError) { Debug.LogError(msg.GetError().Message, this); return; } AssetFileDownloadResult result = msg.GetAssetFileDownloadResult(); Debug.Log("Asset file downloaded to: " + result.Filepath); // Parse Addressable key string[] temp = result.Filepath.Split('\\'); string key = temp[temp.Length - 1]; // Add filepath and parsed key to the Addressable resource locators List <ResourceLocationData> locationData = new List <ResourceLocationData>(); locationData.Add(new ResourceLocationData( new string[] { key }, result.Filepath, typeof(AssetBundleProvider), typeof(AssetBundle))); ResourceLocationMap locMap = new ResourceLocationMap(result.Filepath, locationData); Addressables.AddResourceLocator(locMap); // if (new AssetReference(AssetDatabase.AssetPathToGUID(newPath)).RuntimeKeyIsValid()) // Debug.Log("F*****G AYYYYYYY MATE"); // else // Debug.LogError("Invalid AssetReference", this); PopulateAssetList(); }
private IEnumerator UpdateResourceLocatorsAsync() { Addressables.ClearResourceLocators(); string[] catalogs = null; if (Directory.Exists(addressablesDir)) { catalogs = GetCatalogs(addressablesDir, "json", SearchOption.TopDirectoryOnly); for (int i = 0; i < catalogs.Length; i++) { var catalogHandle = Addressables.LoadContentCatalogAsync(catalogs[i]); yield return(catalogHandle); Addressables.AddResourceLocator(catalogHandle.Result); Addressables.Release(catalogHandle); } } if (catalogs == null || catalogs.Length == 0) { catalogs = GetCatalogs(Addressables.RuntimePath, "json", SearchOption.TopDirectoryOnly); for (int i = 0; i < catalogs.Length; i++) { var catalogHandle = Addressables.LoadContentCatalogAsync(catalogs[i]); yield return(catalogHandle); Addressables.AddResourceLocator(catalogHandle.Result); Addressables.Release(catalogHandle); } } }
public async UniTask Load(string domain, string catalogName) { var uri = $"{domain}/{PlatformMappingService.GetPlatform()}/{catalogName}"; while (true) { var operation = Addressables.LoadContentCatalogAsync(uri); await operation.Task; if (operation.OperationException != null) { var errorResult = await errorRequester.Request(ErrorMessage.Create(ErrorStatus.Fatal, operation.OperationException)); if (errorResult == ErrorResult.Retry) { continue; } else if (errorResult == ErrorResult.Error) { throw operation.OperationException; } } Addressables.AddResourceLocator(operation.Result); } }
private void Cloud_Completed(AsyncOperationHandle <IResourceLocator> obj) { Addressables.AddResourceLocator(obj.Result); Debug.LogError(obj.Result); Debug.LogError(obj.Result.Keys); Debug.LogError(obj.Result.LocatorId); map = (ResourceLocationMap)obj.Result; contentCatalogLoaded = true; }
public static IEnumerator Start() { float startTime = Time.realtimeSinceStartup; #if (UNITY_EDITOR && !USE_CDN) || DISABLE_CDN yield break; #endif //初始化AAS yield return(Addressables.InitializeAsync()); LogTimeDurFormat("Addressables.InitializeAsync", ref startTime, true); //修改provider fixed AAS bug foreach (var provider in Addressables.ResourceManager.ResourceProviders) { if (typeof(TextDataProvider).IsAssignableFrom(provider.GetType())) { (provider as TextDataProvider).IgnoreFailures = false; } } LogTimeDurFormat("foreach Addressables.ResourceManager.ResourceProviders ", ref startTime, true); //拉取本地资源版本号 var localVersion = Application.version; if (File.Exists(localVersionPath)) { var localVersionHandle = LoadText(localVersionPath, "LocalVersion");; yield return(localVersionHandle); localVersion = localVersionHandle.Result; Addressables.Release(localVersionHandle); } var localResVersion = ResVersion.parseVersion(localVersion); var libResVersion = ResVersion.parseVersion(Application.version); LogFormat("local version:{0},library version:{1}", localVersion, Application.version); if (null == localResVersion || null == libResVersion) { yield break; } //包内版本大于本地持续化版本号(安装了新包) 清除本地缓存数据 if (libResVersion.high > localResVersion.high || libResVersion.mid > localResVersion.mid) { LogFormat("清除本地缓存"); Caching.ClearCache(); File.Delete(localCatalogPath); File.Delete(localVersionPath); File.Delete(PersistCDNConfigPath); if (Directory.Exists(LocalHotAssetBundlePath)) { Directory.Delete(LocalHotAssetBundlePath, true); } //yield break; } LogTimeDurFormat("load localVersion", ref startTime, true); //读取CDN 地址配置 yield return(LoadCDNConfig()); LogTimeDurFormat("load cdn config", ref startTime, true); //拉取CDN资源版本号 var remoteVersionHandle = LoadText(remoteVersionUrl, "RemoteVersion"); yield return(remoteVersionHandle); var remoteVersion = remoteVersionHandle.Result; Addressables.Release(remoteVersionHandle); LogTimeDurFormat("load remoteVersion", ref startTime, true); if (null == remoteVersion) { LogErrorFormat("远程资源版本号加载失败"); yield break; } LogFormat("remoteVersion:{0}", remoteVersion); var remoteResVersion = ResVersion.parseVersion(remoteVersion); if (null == remoteResVersion) { yield break; } if (remoteResVersion.high < localResVersion.high || (remoteResVersion.high == localResVersion.high && remoteResVersion.mid < localResVersion.mid)) { LogFormat("服务器资源版本过低"); yield break; } if (remoteResVersion.high > localResVersion.high || remoteResVersion.mid > localResVersion.mid) { LogFormat("需要更新整包"); yield break; } else if (remoteResVersion.small > localResVersion.small) { LogFormat("need hot update"); //拉取CDN catalog 信息 LogFormat("remoteCatalogUrl:" + remoteCatalogUrl); var remoteCatalogHandle = LoadText(remoteCatalogUrl, "RemoteCatalog"); yield return(remoteCatalogHandle); var remoteCatalog = remoteCatalogHandle.Result; Addressables.Release(remoteCatalogHandle); if (null == remoteCatalog) { LogErrorFormat("远程资源加载失败"); yield break; } //将最近的catalog缓存到本地持续化目录 File.WriteAllText(localCatalogPath, remoteCatalog); File.WriteAllText(localVersionPath, remoteVersion); LogTimeDurFormat("load remote catalog", ref startTime, true); } LogFormat("localCatalogPath:" + localCatalogPath); //本地持续化目录没有缓存catalog if (!File.Exists(localCatalogPath)) { yield break; } //对比catalog 解析出需要热更的bundle LogFormat("localCatalogPath:" + localCatalogPath); var localCatalogLocation = new ResourceLocationBase("LocalCatalog", localCatalogPath, typeof(JsonAssetProvider).FullName, typeof(ContentCatalogData)); var localCatalogHandle = Addressables.LoadAssetAsync <ContentCatalogData>(localCatalogLocation); yield return(localCatalogHandle); var data = localCatalogHandle.Result; Addressables.Release(localCatalogHandle); LogTimeDurFormat("load local catalog", ref startTime, true); ResourceLocationMap RemoteMap = data.CreateLocator(null); var newBundles = parseBundleList(RemoteMap, "remote"); LogTimeDurFormat("foreach remote catalog", ref startTime, true); dirtyBundle = null; // Addressables.CheckForCatalogUpdates(false); foreach (ResourceLocationMap item in Addressables.ResourceLocators) { if ("AddressablesMainContentCatalog" == item.LocatorId) { var oldBundles = parseBundleList(item, "local"); LogTimeDurFormat("load local catalog", ref startTime, true); dirtyBundle = compareResLocation(newBundles, oldBundles); Addressables.RemoveResourceLocator(item); Addressables.AddResourceLocator(RemoteMap); LogTimeDurFormat("compare catalog", ref startTime, true); break; } } if (null == dirtyBundle || 0 == dirtyBundle.Count) { yield break; } //该方法计算下载大小只对远程bundle有用 //long size = 0; //foreach (var key in dirtyBundle) //{ // IResourceLocation location; // newBundles.TryGetValue(key, out location); // var sizeData = location.Data as ILocationSizeData; // if (sizeData != null) // size += sizeData.ComputeSize(location, Addressables.ResourceManager); //} // LogFormat("需要下载的补丁大小:{0}", size); yield return(DownLoadRemoteAssetBundles(dirtyBundle)); LogTimeDurFormat("dowmload hot file", ref startTime, true); Addressables.InternalIdTransformFunc = convertBundleInternalId; LogFormat("下载完,预加载到内存"); AsyncOperationHandle handler = Addressables.DownloadDependenciesAsync(dirtyBundle, Addressables.MergeMode.Union); while (!handler.IsDone) { yield return(null); } Addressables.Release(handler); LogTimeDurFormat("download dependency", ref startTime, true); yield return(null); }
/// <summary> /// Add a resource locator. /// </summary> /// <param name="locator">The locator object.</param> /// <param name="localCatalogHash">The hash of the local catalog. This can be null if the catalog cannot be updated.</param> /// <param name="remoteCatalogLocation">The location of the remote catalog. This can be null if the catalog cannot be updated.</param> public static void AddResourceLocator(IResourceLocator locator, string localCatalogHash = null, IResourceLocation remoteCatalogLocation = null) { Addressables.AddResourceLocator(locator, localCatalogHash, remoteCatalogLocation); }