private IEnumerator SendDownloadFileCoroutine(string interfaceName, params string[] parameter) { StringBuilder builder = new StringBuilder(); builder.Append(WebInterfaces[interfaceName].Url); if (parameter.Length > 0) { builder.Append("?"); builder.Append(parameter[0]); } for (int i = 1; i < parameter.Length; i++) { builder.Append("&"); builder.Append(parameter[i]); } string url = builder.ToString(); using (UnityWebRequest request = UnityWebRequest.Get(url)) { DateTime begin = DateTime.Now; WebInterfaceDownloadFile wi = WebInterfaces[interfaceName] as WebInterfaceDownloadFile; wi.OnSetDownloadHandler(request); request.SendWebRequest(); while (!request.isDone) { wi.OnLoading(request.downloadProgress); yield return(null); } wi.OnLoading(1f); yield return(null); DateTime end = DateTime.Now; if (!request.isNetworkError && !request.isHttpError) { Log.Info(string.Format("[{0}] 发起下载文件请求:[{1}] {2}\r\n[{3}] 成功下载至:{4}" , begin.ToString("mm:ss:fff"), interfaceName, url, end.ToString("mm:ss:fff"), wi.Path)); wi.OnFinished(true); } else { Log.Error(string.Format("[{0}] 发起下载文件请求:[{1}] {2}\r\n[{3}] 下载失败:{4}" , begin.ToString("mm:ss:fff"), interfaceName, url, end.ToString("mm:ss:fff"), request.error)); wi.OnFinished(false); } } }
/// <summary> /// 注册接口(获取 File) /// </summary> /// <param name="interfaceName">接口名称</param> /// <param name="interfaceUrl">接口url</param> /// <param name="savePath">保存路径</param> /// <param name="loadingHandler">下载过程中回调</param> /// <param name="finishedHandler">下载完成回调</param> /// <param name="offlineHandle">离线模式处理者</param> public void RegisterInterface(string interfaceName, string interfaceUrl, string savePath, HTFAction <float> loadingHandler, HTFAction <bool> finishedHandler, HTFAction offlineHandle) { if (!WebInterfaces.ContainsKey(interfaceName)) { WebInterfaceDownloadFile wi = Main.m_ReferencePool.Spawn <WebInterfaceDownloadFile>(); wi.Name = interfaceName; wi.Url = interfaceUrl; wi.OfflineHandler = offlineHandle; wi.LoadingHandler = loadingHandler; wi.FinishedHandler = finishedHandler; wi.Path = savePath; WebInterfaces.Add(interfaceName, wi); } else { Log.Error(string.Format("注册接口失败:已存在名为 {0} 的网络接口!", interfaceName)); } }
/// <summary> /// 注册接口(获取 File) /// </summary> /// <param name="interfaceName">接口名称</param> /// <param name="interfaceUrl">接口url</param> /// <param name="savePath">保存路径</param> /// <param name="loadingHandler">下载过程中回调</param> /// <param name="finishedHandler">下载完成回调</param> /// <param name="offlineHandle">离线模式处理者</param> public void RegisterInterface(string interfaceName, string interfaceUrl, string savePath, HTFAction <float> loadingHandler, HTFAction <bool> finishedHandler, HTFAction offlineHandle) { if (!WebInterfaces.ContainsKey(interfaceName)) { WebInterfaceDownloadFile wi = Main.m_ReferencePool.Spawn <WebInterfaceDownloadFile>(); wi.Name = interfaceName; wi.Url = interfaceUrl; wi.OfflineHandler = offlineHandle; wi.LoadingHandler = loadingHandler; wi.FinishedHandler = finishedHandler; wi.Path = savePath; WebInterfaces.Add(interfaceName, wi); } else { throw new HTFrameworkException(HTFrameworkModule.WebRequest, "添加接口失败:已存在名为 " + interfaceName + " 的网络接口!"); } }