Example #1
0
        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);
        }
Example #2
0
 /// <summary>
 /// Remove a locator;
 /// </summary>
 /// <param name="locator">The locator to remove.</param>
 public static void RemoveResourceLocator(IResourceLocator locator)
 {
     Addressables.RemoveResourceLocator(locator);
 }