Unity-Http协议请求下载系列1:接口封装HttpHelper

游戏请求服务器下载资源,是很常见的操作。特别是有热更的资源游戏,游戏启动会检查下载Bundle资源。使用的是Http协议。本人查看很多资料,实现服务器请求下载资源的方式大概有2种,其一是NET框架提供的HttpWebRequest接口,另外就是Unit引擎提供的UnityWebRequest接口。另外,是否支持断点续传。本人整合封装了上述2中实现方式,并且支持完全重新下载和断点续传2种下载模式。

    这篇文章主要介绍实现Http请求的接口封装类:HttpHelper

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
 * Author:W
 * Web请求工具
 */
namespace W.GameFramework.HotUpdate
{
	/// 
	/// 下载方式:重新或者断点续传
	/// 
	public enum DownloadType
	{
		New,//重新下载
	    Continue,//断点续传下载
	}
	/// 
	/// 开始下载回调
	/// 
	public delegate void OnDownloadBeginHandler();
	/// 
	/// 下载进度回调
	/// 
	/// 
	/// 
	public delegate void OnDownloadProgressHanlder(float progress);
	/// 
	/// 下载结束回调
	/// 
	public delegate void OnDownloadEndHandler();
	/// 
	/// 下载错误
	/// 
	public delegate void OnDownloadErrorHandler(string errorCode);

	public  class HttpHelper
	{
		/// 
		/// 当前下载资源资源URL地址
		/// 
		public  string url;
		/// 
		/// 当前下载资源的保存路径
		/// 
		public string path;


		private float _progress = 0f;
		/// 
		/// 当前下载的进度
		/// 
		protected float progress
		{
			get { return _progress; }

			set 
			{
				_progress = value;

				if (OnDownloadProgressHanlder != null)
					OnDownloadProgressHanlder(value);
			}
		}

		/// 
		/// 文件当前已下载的长度
		/// 
		protected long fileCurLength = 0;
		/// 
		/// 当前下载文件的总长度
		/// 
		protected long fileLength = 0;


		/// 
		/// 请求时间
		/// 
		protected int timeOut = 100000;
		/// 
		/// 请求方式
		/// 
		protected string method = "GET";

		private bool _isStop = false;
		/// 
		/// 是否暂停
		/// 
		public bool IsStop
		{
			get { return this._isStop; }

			set { this._isStop = value; }
		}

		/// 
		/// 请求下载方式
		/// 
		protected DownloadType downloadType = DownloadType.New;

		/// 
		/// 下载开始回调
		/// 
		protected OnDownloadBeginHandler OnDownloadBeginHandler;
		/// 
		/// 下载结束回调
		/// 
		protected OnDownloadEndHandler OnDownloadEndHandler;

		/// 
		/// 下载进度回调
		/// 
		protected OnDownloadProgressHanlder OnDownloadProgressHanlder;

		/// 
		/// 下载错误异常回调
		/// 
		protected OnDownloadErrorHandler OnDownloadErrorHandler;



		/// 
		/// 向服务器发出请求下载url资源
		/// 
		/// 资源网络地址
		/// 资源保存路径
		/// 下载方式:重新下载或断点续传
		/// 下载开始回调
		/// 下载完成回调
		/// 下载错误回调
		public virtual void SendHttpRequest(string url, string path,DownloadType downloadType = DownloadType.New, OnDownloadBeginHandler onDownloadBeginHandler = null, OnDownloadEndHandler onDownloadEndHandler = null,
			OnDownloadProgressHanlder onDownloadProgressHanlder = null,OnDownloadErrorHandler onDownloadErrorHandler = null)
		{
			this.url = url;
			this.path = path;
			this.downloadType = downloadType;
			this.OnDownloadBeginHandler = onDownloadBeginHandler;
			this.OnDownloadEndHandler = onDownloadEndHandler;
			this.OnDownloadProgressHanlder = onDownloadProgressHanlder;
			this.OnDownloadErrorHandler = onDownloadErrorHandler;
		}

		/// 
		/// 清理
		/// 
		public virtual void Clear() { }
	}
}

下篇文章链接https://blog.csdn.net/wlqchengzhangji/article/details/118704311

你可能感兴趣的:(Unity,C#,Unity,Http,HttpWebRequest,UnityWebRequest,断点续传)