Example #1
0
        /// <summary>
        /// 下载成功处理
        /// </summary>
        private void DownloadSuccessExecute(HKDownloadTaskItem _item, object _data)
        {
            if (null != _item)
            {
                _item.DownloadResult = DownloadResult.Success;

                updateInfo.CurrentProgress = 100; // 更新一次满进度,主要考虑到UI相关的更新
                if (null != downloadUpateInfoAction)
                {
                    downloadUpateInfoAction(updateInfo);
                }

                if (_item.DownloadResType == DownloadResType.StringContent) // string 类型
                {
                    if (null != _item.CompleteAction)
                    {
                        _item.CompleteAction(_item.Url, _data as string, _item.UserData);
                    }
                }
                else if (_item.DownloadResType == DownloadResType.AssetBundle) // AssetBundle类型
                {
                    if (true == _data is byte[])
                    {
                        // 将数据写入到本地
                        HKRequestTools.WriteAssetBundleToLcoal(_item.Url, (byte[])_data);

                        // 处理回调
                        if (null != _item.CompleteAction)
                        {
                            var assetBundle = AssetBundle.LoadFromMemory((byte[])_data);
                            _item.CompleteAction(_item.Url, assetBundle, _item.UserData);
                        }
                    }
                }
                else if (_item.DownloadResType == DownloadResType.ByteArray)  // 二进制类型
                {
                    if (true == _data is byte[])
                    {
                        // 处理回调
                        if (null != _item.CompleteAction)
                        {
                            _item.CompleteAction(_item.Url, _data, _item.UserData);
                        }
                    }
                }

                // 使用完后,就回收到内存池中
                HKDownloadTaskItem.downloadItemCache.Recover(_item);
            }

            // 检测当前剩余下载对象,并继续下载
            CheckQueueComplete();
        }
        /// <summary>
        /// 加载一个Asset Bundle,加载完后,会自动写入到可读写目录下
        /// 如果存在相同的文件,会被覆盖
        /// </summary>
        /// <param name="_url"></param>
        /// <param name="_progrss"></param>
        /// <param name="_compolete"></param>
        public void GetAssetBundle(string _url, Action <AssetBundle> _compolete = null, Action <int> _progrss = null)
        {
            if (false == HKRequestTools.IsCheckUrl(_url))
            {
                return;
            }
            cacheBytes.Clear();
            httpClient.GetByteArray(new Uri(_url), HttpCompletionOption.StreamResponseContent, response =>
            {
                if (null != _progrss)
                {
                    _progrss(response.PercentageComplete);
                }
                if (null != response.Data)
                {
                    cacheBytes.AddRange(response.Data);
                }
                if (response.PercentageComplete >= 100 && true == response.IsSuccessStatusCode)
                {
                    byte[] byteContent = cacheBytes.ToArray();

                    // 写入本地
                    HKRequestTools.WriteAssetBundleToLcoal(_url, byteContent);

                    //  如果需要回调,则创建一个AssetBundle进行回调
                    if (null != _compolete)
                    {
                        var assetBundle = AssetBundle.LoadFromMemory(byteContent);
                        _compolete(assetBundle);
                    }

                    // 清空队列
                    cacheBytes.Clear();
                }
            });
        }