Example #1
0
    void DownloadNextFile()
    {
        //Debug.Log(downloadFileIndex + " : " + totalFileCount);
        //下载完成
        if (downloadFileIndex >= totalFileCount)
        {
            FinishDownloadFile();
            return;
        }
        //更新进度
        if (downloadUpdate != null)
        {
            downloadUpdate(downloadFileIndex + 1, totalFileCount);
        }

        Debug.Log(downloadList[downloadFileIndex]);
        string bundleName     = downloadList[downloadFileIndex];
        string bundleFullName = remoteResourceData.GetBundleFullNameByBundleName(bundleName);
        //先使用中间文件
        string localFilePath  = PathTools.DataPath + bundleFullName + ".temp";
        string remoteFilePath = UpdateConfig.Instance.serverUrl + bundleFullName;

        Debug.Log(remoteFilePath);
        float progress = 0f;

        //开启子线程下载,使用匿名方法
        Loom.RunAsync(() =>
        {
            //使用流操作文件
            FileStream fs = new FileStream(localFilePath, FileMode.OpenOrCreate, FileAccess.Write);
            //获取文件现在的长度
            long fileLength = fs.Length;
            //获取下载文件的总长度
            long totalLength = remoteResourceData.GetSizeByBundleName(bundleName);

            //如果没下载完
            if (fileLength < totalLength)
            {
                //断点续传核心,设置本地文件流的起始位置
                fs.Seek(fileLength, SeekOrigin.Begin);

                HttpWebRequest request = HttpWebRequest.Create(remoteFilePath) as HttpWebRequest;

                //断点续传核心,设置远程访问文件流的起始位置
                request.AddRange((int)fileLength);
                Stream stream = request.GetResponse().GetResponseStream();

                byte[] buffer = new byte[1024];
                //使用流读取内容到buffer中
                //注意方法返回值代表读取的实际长度,并不是buffer有多大,stream就会读进去多少
                int length = stream.Read(buffer, 0, buffer.Length);
                while (length > 0)
                {
                    //将内容再写入本地文件中
                    fs.Write(buffer, 0, length);
                    //计算进度
                    fileLength += length;
                    progress    = (float)fileLength / (float)totalLength;
                    //类似尾递归
                    length = stream.Read(buffer, 0, buffer.Length);

                    //更新下载大小
                    Loom.QueueOnMainThread(() =>
                    {
                        if (sizeUpdate != null)
                        {
                            sizeUpdate(finishFileSize + fileLength, totalFileSize);
                        }
                    });
                }
                stream.Close();
                stream.Dispose();
                fs.Close();
            }
            else
            {
                progress = 1;
            }

            //如果下载完毕,执行回调
            if (progress == 1)
            {
                Debug.Log(bundleName + " Download finished!");
                Loom.QueueOnMainThread(() =>
                {
                    finishFileSize += remoteResourceData.GetSizeByBundleName(bundleName);
                    ++downloadFileIndex;
                    DownloadNextFile();
                });
            }
            else
            {
                //多次重试 失败后 直接开始游戏
                if (downloadRetryCount > 4)
                {
                    Loom.QueueOnMainThread(() =>
                    {
                        EndUpdateResource();
                    });
                    return;
                }

                //下载出错了  再次下载
                Loom.QueueOnMainThread(() =>
                {
                    ++downloadRetryCount;
                    DownloadNextFile();
                });
                return;
            }
        });
    }