Пример #1
0
    private void OnDownloadState(GameAsyncResInfo resInfo, GameResDownloadState state, float progress, int downloadSize)
    {
        if (resInfo != null)
        {
            FTDebug.LogWarning($"SingleAsnycRes Download State [{resInfo.bundleName}] [{state}] [{progress}] [{downloadSize}]");
        }
        if (state == GameResDownloadState.SUCCESS && resInfo != null)
        {
            GameAsyncRes.OnAsyncResDownloadSuccess(resInfo.bundleName);
        }
        var iState = (int)state;

        if (_onDownloadState != null)
        {
            _onDownloadState(iState, progress, downloadSize);
        }
        if (_downloadEvent != null)
        {
            if (_isDetailEvent || iState >= (int)GameResDownloadState.SUCCESS || iState != _lastEventState)
            {
                _downloadEvent(iState, progress, downloadSize);
                _lastEventState = iState;
            }
        }
        if (state == GameResDownloadState.SUCCESS || state == GameResDownloadState.FAILED)
        {
            _onDownloadState = null;
        }
    }
Пример #2
0
    private void StartDownload(bool isResume = false, int tryTimes = 0)
    {
        if (_isPaused)
        {
            return;
        }

        if (_curDownloadIndex >= _totalCount)
        {
            OnDownloadState(null, GameResDownloadState.SUCCESS, _totalCount, _totalCount, 0, _downloadedSize, _totalSize);
            return;
        }

        if (isResume)
        {
            _lastDownloadIndex = -1;
            // 暂停并不停止当前下载任务,只是不通知上层,若取消暂停,需看下当前任务是否完成,未完成的话不再开始新任务
            var curTask = _downloader.GetTask(_curTaskId);
            if (curTask != null && curTask.state < GameResDownloadState.SUCCESS)
            {
                return;
            }
        }

        var curRes = _allAsyncRes[_curDownloadIndex];

        OnDownloadState(curRes, GameResDownloadState.DOWNLOADING, _curDownloadIndex, _totalCount, 0, _downloadedSize, _totalSize);
        _curTaskId = _downloader.Start(curRes.bundleName, curRes.urls, curRes.hash, curRes.size, (state, progress, downloadSize, leftCount) =>
        {
            var eState = (GameResDownloadState)state;
            switch (eState)
            {
            case GameResDownloadState.SUCCESS:
                {
                    GameResLoader.INSTANCE.LoadAssetBundle(curRes.bundleName, curRes.manifestName, curRes.hash, curRes.urls, false, (isSuccess) =>
                    {
                        if (isSuccess)
                        {
                            GameAsyncRes.OnAsyncResDownloadSuccess(curRes.bundleName);
                            OnDownloadState(curRes, (GameResDownloadState)state, _curDownloadIndex, _totalCount, downloadSize, _downloadedSize, _totalSize);
                        }

                        _downloadedSize += downloadSize;
                        _curDownloadIndex++;
                        StartDownload();
                    });
                }
                break;

            case GameResDownloadState.FAILED:
                {
                    if (tryTimes < TASK_TRY_TIMES)
                    {
                        StartDownload(false, tryTimes++);
                        return;
                    }
                    OnDownloadState(curRes, GameResDownloadState.FAILED, _curDownloadIndex, _totalCount, downloadSize, _downloadedSize, _totalSize);
                }
                break;

            default:
                {
                    OnDownloadState(curRes, (GameResDownloadState)state, _curDownloadIndex, _totalCount, downloadSize, _downloadedSize, _totalSize);
                }
                break;
            }
        }, 10, true);
    }