文章目录
- 前提配置
- 代码实现
前提配置
(1)勾选AddressableAssetSettings设置的Disable Catalog Update On Startup选项
(2)相应的热更戏资源分组配置(注:此文采用的是动态资源更新)
Can Change Post Release:No-Static(全量更新资源,直接替换旧资源)
Cannot Change Post Release:Static(增量更新,不更新旧包资源,直接生成新的资源包)
代码实现
(1)如果本地已经确定好了具体需要热更的资源,可以将需要更新资源的标签记录到本地,每次游戏启动时,做热更新检查,本地系统会对比远程与本地资源的Catalog,不同则更新
(2)有服务器下发热更新资源的名称或标签,同上
(3)暴力热更,获取全部Adressable资源的标签,逐一检车是否需要更新,当然这种方式是不建议的
/// <summary>/// 暴力获取所有Key/// </summary>public void HotUpdate(){IEnumerable<IResourceLocator> locators = Addressables.ResourceLocators;List<object> keys = new List<object>();foreach (var item in locators){foreach (var key in item.Keys){keys.Add(key);Debug.Log("Key:"+key);}}Debug.Log("Keys:" + keys.Count);if (keys.Count > 0)StartCoroutine(CheckForContentUpdate(keys));}
热更新核心代码
/// <summary>/// 检查更新/// </summary>/// <param name="keys">确保Keys内标签都需要热更,否则,所有的Key资源都会被下载,并且删除旧资源时,真实更新key的旧资源不会被删除</param>/// <returns></returns>public IEnumerator CheckForContentUpdate(List<object> keys){for (int index = 0; index < keys.Count; index++){AsyncOperationHandle<long> DownloadSize = Addressables.GetDownloadSizeAsync(keys[index]);yield return DownloadSize;if (DownloadSize.Result <= 0){Debug.Log("没有更新的资源标签:" + keys[index]);keys.Remove(keys[index]);}else{m_TotalSize += DownloadSize.Result / Mathf.Pow(1024, 2);}}if (keys.Count <= 0)yield return null;m_DownLoadKeyList = keys;m_DownloadDependencies = Addressables.DownloadDependenciesAsync(keys, Addressables.MergeMode.Union, false);yield return m_DownloadDependencies;}private void Update(){if (m_DownloadDependencies.IsValid()){if (m_DownloadDependencies.PercentComplete < 1){m_CurAssetSize = m_DownloadDependencies.PercentComplete * m_TotalSize;Debug.Log(m_CurAssetSize + "/" + m_TotalSize);}else if (m_DownloadDependencies.Status == AsyncOperationStatus.Succeeded){m_CurAssetSize = m_DownloadDependencies.PercentComplete * m_TotalSize;Debug.Log(m_CurAssetSize + "/" + m_TotalSize);if (m_DownLoadKeyList.Count > 0){//删除旧资源包,前提Bundle name选择FileName,aa管理AB包名是带Hash的,每个版本的包名都不一致,导致Cache无法区分Addressables.ClearDependencyCacheAsync(m_DownLoadKeyList);}Addressables.Release(m_DownloadDependencies);}}}