使用Adressable完成游戏更新-检测并更新

需要扩展两个方法出来,一个是修改请求catalog的地址,一个是获取Bundle的大小接口

//Addressables.cs
public static void SetRemoteCatalogLocation(string newLocation)
{
    m_Addressables.SetRemoteCatalogLocation(newLocation);
}

public static long GetResourceLocationSize(IResourceLocation location)
{
    return m_Addressables.GetResourceLocationSize(location);
}

//AddressablesImpl.cs
public void SetRemoteCatalogLocation(string location)
{
    for (var i = 0; i < m_ResourceLocators.Count; i++)
    {
        if (m_ResourceLocators[i].CanUpdateContent)
        {
            var locationInfo = m_ResourceLocators[i] as ResourceLocatorInfo;
            if (locationInfo.HashLocation != null)
            {
                locationInfo.HashLocation.InternalId = location;

            }
        }
    }
}

public long GetResourceLocationSize(IResourceLocation location)
{
    long size = 0;
    var sizeData = location.Data as ILocationSizeData;
    if (sizeData != null)
        size = sizeData.ComputeSize(location, ResourceManager);

    return size;
}


更新流程:

  1. 设置加载资源的远程路径,catalog和Bundle

  2. 检查网络资源表catalog

  3. 下载网络资源表catalog

  4. 通过全局资源lable后去所有的ResourceLocation

  5. 自定义辨别筛选出需要更新Bundle资源的ResourceLocation

  6. 获取下载的大小

  7. 更新下载


    //初始化内置Bundle包列表 AssetBundleManager.cs
    public void Init()
    {
#if !UNITY_EDITOR
            var json = Resources.Load<TextAsset>("Version/BuildInBundleName");
            buildInData = JsonUtility.FromJson<BuildInBundleData>(json.text);
#endif
    }

    
//AddressableManager.cs
//1.设置远程加载地址
public void SetAddressableRemoteResCdnUrl(string remoteUrl)
{
    Debug.Log("SetAddressableRemoteUrl remoteUrl = " + remoteUrl);
    if (string.IsNullOrEmpty(remoteUrl))
    {
        return;
    }


    //设置catalog的请求路径
    string newLocation = remoteUrl + "/" + "catalog_999.hash";//后缀对应为AddressableAssetSettings的PlayerVersionOverried
    Addressables.SetRemoteCatalogLocation(newLocation);

    //设置location的transfrom func
    Addressables.InternalIdTransformFunc = (IResourceLocation location) =>
    {
        string internalId = location.InternalId;
        if (internalId != null && internalId.StartsWith("http"))
        {
            var fileName = Path.GetFileName(internalId);
            string newInternalId = remoteUrl + "/" + fileName;
            return newInternalId;
        }
        else
        {
            return location.InternalId;
        }
    };
}

//2.检查网络资源表
public OperationData CheckForCatalogUpdates()
{
    var aOperation = Addressables.CheckForCatalogUpdates(false);
    return OperationData.Get(aOperation);
}

//3.下载对应资源表
public OperationData UpdateCatalogs(List<string> catlogs)
{
    var aOperation = Addressables.UpdateCatalogs(catlogs,false);
    return OperationData.Get(aOperation);
}

//4.通过全局lable获取所有的ResourceLocation
public OperationData GetCheckContentList(string label)
{
    CheckContentList.Clear();
    var aOperation = Addressables.LoadResourceLocationsAsync(new List<string> { label }, Addressables.MergeMode.Union);
    return OperationData.Get(aOperation,false, OnGetCheckContentList);
}

//5.筛选出需要更新的内容 -1错误 0为没有更新
public int GetUpdateContentList()
{
    if (CheckContentList == null)
    {
        Debug.LogError("获取所有的ResourceLocation为空");
        return -1;
    }

    if (AssetBundleManager.GetInstance().buildInData == null)
    {
#if !UNITY_EDITOR
        Debug.LogError("获取内置Bundle配置为空");
        return -1;
#else
        return 0;
#endif
    }
    IList<IResourceLocation> updateContent = new List<IResourceLocation>();
    foreach (var item in CheckContentList)
    {
        string bundleName;
        if (item.HasDependencies)
        {
            foreach (var dep in item.Dependencies)
            {
                bundleName = Path.GetFileName(dep.InternalId);
                if (AssetBundleManager.GetInstance().buildInData.BuildInBundleNames.Contains(bundleName))
                {

                }
                else if (AssetBundleManager.GetInstance().IsCache(bundleName))
                {
                }
                else
                {
                    updateContent.Add(dep);
                }
            }
        }
    }

    CheckContentList = updateContent;

    return CheckContentList.Count;
}

//6.获取需要下载的大小
public long GetDownloadSize(string label)
{
    long size = 0;
    foreach (IResourceLocation location in CheckContentList.Distinct())
    {
        size += Addressables.GetResourceLocationSize(location);
    }

    return size;
}

//7.下载更新
public OperationData DownloadDependenciesAsync(string label)
{
    var aOperation = Addressables.DownloadDependenciesAsync(CheckContentList);
    return OperationData.Get(aOperation);
}

public void OnGetCheckContentList(object list)
{
    CheckContentList = list as IList<IResourceLocation>;
}


OperationData为继承IEnumerator,提供异步等待


首页 我的博客
粤ICP备17103704号