Addressable hot update

Article directory

Prerequisite configuration

(1) Check the Disable Catalog Update On Startup option set by AddressableAssetSettings
Insert image description here
(2) Corresponding hot update resource grouping configuration (Note: This article uses dynamic resource update )
Can Change Post Release: No-Static (Update resources in full, directly replace old resources)
Cannot Change Post Release: Static (Incremental update, no update Old package resources, directly generate new resource packages)
Insert image description here

Code

(1) If the local resources that need to be hot updated have been determined, the tags of the resources that need to be updated can be recorded locally. Every time the game is started, a hot update check is performed, and the local system will compare the remote and local ones. Catalog of resources, update if different
(2) If there is a hot update resource name or label under the server, the same as above
(3) Violent hot update, get all Adressable Tags of resources, check each vehicle one by one to see if it needs to be updated. Of course, this method is not recommended

/// <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));
    }

Hot update core code

/// <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);
            }
        }
    }

Guess you like

Origin blog.csdn.net/weixin_42186644/article/details/118000390