Beispiel #1
0
        // 6.下载所有更新文件
        private IEnumerator DownloadAssets()
        {
            //	Debug.Log("Updater.DownloadAssets " + _updateList.Count);

            Directory.CreateDirectory(GameConfig.PERSISTENT_PATH + "media");

            AssetIndexFile oldAssets = AssetIndexData.Instance().GetIndex();

            int accumSize = 0;

            _downloadedSize = 0;
            UpdateProgress();

            string rootUrl = _updateSetting.resUrl + _latestVersion + '-' + GameConfig.PLATFORM + '/';

            foreach (var asset in _updateList)
            {
                byte[] bytes = null;

                _currentAssetSize       = asset.size;
                _currentAssetDownloaded = 0;

                string url = rootUrl + asset.GetFilename() + "?spm=" + GetRandomValue();
                for (int i = 0; i < DOWNLOAD_RETRY_TIMES; i++)
                {
                    WWW downloader = new WWW(url);
                    _downloader = downloader;
                    yield return(downloader);

                    // 下载成功
                    if (downloader.error == null)
                    {
                        bytes = downloader.bytes;
                        break;
                    }

                    yield return(new WaitForSeconds(DOWNLOAD_RETRY_DELAY));
                }

                if (bytes == null || bytes.Length != asset.size)
                {
                    if (bytes == null)
                    {
                        Debug.LogWarning("Updater.DownloadAssets failed " + url);
                    }
                    else
                    {
                        Debug.LogWarning("Updater.DownloadAssets invalid size of " + url);
                    }

                    // 下载更新包失败,弹出重试窗口
                    _confirmDownload = false;
                    ShowNoticeDialog(GetText(MESSAGE_NETWORK_ERROR), BUTTON_RETRY, OnRetryCallback);
                    yield break;
                }

                // 写入文件
                File.WriteAllBytes(asset.GetWritePath(), bytes);

                _currentAssetSize       = 0;
                _currentAssetDownloaded = 0;
                _downloader             = null;

                _downloadedSize += asset.size;
                accumSize       += asset.size;
                UpdateProgress();

                // 替换索引
                oldAssets.AddAssetInfo(asset);

                // 凑齐若干字节后写入索引文件
                if (accumSize >= DOWNLOAD_ACCUM_WRITE_INDEX)
                {
                    accumSize = 0;
                    AssetIndexData.Instance().Save();
                }
            }

            oldAssets.SetVersion(_latest.GetVersion());
            AssetIndexData.Instance().Save();

            GameConfig.VERSION = AssetIndexData.Instance().GetVersionString();

            SetProgressInfo(1, GetText(MESSAGE_COMPLETE), false);
            _updateStatus = STATUS_COMPLETE;
            NotifyUpdateEnd();
        }