Exemple #1
0
    /// <summary>
    /// 下载保存ab文件
    /// </summary>
    /// <param name="url"></param>
    /// <param name="downFileList"></param>
    /// <returns></returns>
    private IEnumerator DownUpdataFile(string url, string localPath, List <AssetBundleManifestData> downFileList)
    {
        for (int i = 0; i < downFileList.Count; i++)
        {
            AssetBundleManifestData data    = downFileList[i];
            UnityWebRequest         request = UnityWebRequest.Get(url + data.fileName);
            yield return(request.SendWebRequest());

            if (request.isHttpError || request.isNetworkError)
            {
                Debug.LogError(request.downloadHandler.text);
                Debug.LogError("下载AB文件时报错:" + data.fileName);
            }
            else
            {
                AssetBunildFileIO.SaveAssetBuildFile(localPath, data.fileName, request.downloadHandler.data);
                AssetBunildFileIO.SaveAssetBuildFile(localPath, data.manifestName, data.ToString());
            }
            request.Dispose();
        }
    }
Exemple #2
0
    public IEnumerator Init()
    {
        Dictionary <string, AssetBundleManifestData> webList   = null;
        Dictionary <string, AssetBundleManifestData> localList = null;

        AssetBundleManifestData webMainManifest   = null;
        AssetBundleManifestData localMainManifest = null;

        Config.Init(target, url);



        //获取本地和服务器的MainManifest清单
        yield return(StartCoroutine(DownMainManifestFile(Config.WebFileMainManifest(), Config.MainManifestFileName(), (d) =>
        {
            webMainManifest = d;
        })));

        yield return(StartCoroutine(DownMainManifestFile(Config.LocalFileMainManifestUnityWebRequest(), Config.MainManifestFileName(), (d) =>
        {
            localMainManifest = d;
        })));

        //网上无MainManifest清单则退出下载程序
        if (webMainManifest.isNO)
        {
            Debug.LogError("网上无AB包!!!");
            yield break;
        }

        //本地无MainManifest清单则创建
        if (!localMainManifest.isNO)
        {
            if (webMainManifest.CRC == localMainManifest.CRC)
            {
                Debug.Log("本地为最新AB包,无需下载");
                yield break;
            }
        }



        //根据主MainManifest文件获取其他manifest清单
        yield return(StartCoroutine(DownAllManifestFile(Config.WebPath(), webMainManifest.abList.Keys.ToList(), (d) =>
        {
            webList = d;
        })));

        yield return
            (StartCoroutine(DownAllManifestFile(Config.LocalFileManifestUnityWebRequest(), localMainManifest.abList.Keys.ToList(),
                                                (d) =>
        {
            localList = d;
        })));



        ///检验获取下载项
        //获取需要下载的列表
        List <AssetBundleManifestData> downList = new List <AssetBundleManifestData>();


        List <string> keys = webList.Keys.ToList();

        for (int i = 0; i < keys.Count; i++)
        {
            string key = keys[i];
            if (webList[key].isNO)
            {
                Debug.LogError("服务器上文件出现问题:" + key);
                continue;
            }

            if (localList.ContainsKey(key))
            {
                if (webList[key].CRC != localList[key].CRC)
                {
                    downList.Add(webList[key]);
                }
                localList.Remove(key);
            }
            else
            {
                downList.Add(webList[key]);
            }
        }

        //根据下载清单 下载并保存清单文件与ab文件
        yield return(StartCoroutine(DownUpdataFile(Config.WebPath(), Config.LocalPath(), downList)));



        //下载文件
        //获取且删除本地无用文件
        List <AssetBundleManifestData> deleteList = localList.Values.ToList();

        for (int i = 0; i < deleteList.Count; i++)
        {
            if (!deleteList[i].isNO)
            {
                AssetBunildFileIO.DeleteABManifest(Config.LocalPath(), deleteList[i].fileName);
            }
        }



        //覆盖本地主清单文件,表示下载完成
        AssetBunildFileIO.SaveAssetBuildFile(Config.LocalPath(), Config.MainManifestFileName(),
                                             webMainManifest.ToString());


        Debug.Log("下载完成");
    }