首页 > 编程知识 正文

unity引擎,unity http

时间:2023-05-06 18:40:51 阅读:141560 作者:747

上一篇文章介绍了一个基于HttpWebRequest接口实现的资源请求下载方案。 本文介绍了一个基于UnityWebRequest接口实现的方案。 然后打开协和式飞机运行。

首先,编写全局协程管理控制器CoroutineManager工具类

using System.Collections; using system.collections.generic; using UnityEngine; /* * Author:W *线程工具【全局】*/namespace w.game framework.hot update { publicclasscoroutinemanager 3360 mono behavion publicstaticcoroutinemanagerinstance { get } if (instance==null ) gameobjectcoroutinemanagerobj=new game object (came object ) game object.dontdestroyonload (coroutinemanagerobj; _ instance=coroutinemanagerobj.addcomponentcoroutinemanager (; }return _instance; }}void Awake () if ) _instance!=null ) game object.destroy (this.game object ); }//summary//打开全局协作函数///summary//param name=' delegate method '/parampublicvoidstartglobalcoroution parampublicvoidstartglobalcoroutine )//summary//全局协管//summary//param name=' delegate method '/parampublibling parampublicvoidstopglobalcoroutine }//summary///所有协同学//summarypublicvoidstopallglobalcoroutines () { stopallcopalcobalcoroutines } }}然后实现核心类UnityWebRequestHelper

using System.Collections; using system.collections.generic; using UnityEngine; using UnityEngine.Networking; using System.IO; /** Author:W* Unity引擎网络请求下载接口封装*/namespace w.game framework.hot update { publicclassunitywebhttpreque privateunitywebrequestwebrequest=null; publicoverridevoidsendhttprequest (string URL,string path,downloadtype=download type.new,ondownloadbeginhadbeginhad ondownloadendhandlerondownloadendhandler=null,ondownloadprogresshanlderondownloadprogresshanlder=null, ondownloaderrorhandlerondownloaderrorhandler=null ) {base.sendler} downloadType,onDownloadBeginHandler,ondownloadend coroutine manager.instance.startglobalcoroutine (sendunitywebhttprequest () ); }

/// <summary> /// 开启协程请求下载资源 /// </summary> /// <returns></returns> IEnumerator SendUnityWebHttpRequest() { //先发一次请求,只获取关于资源大小的Head headRequest = UnityWebRequest.Head(url); yield return headRequest.SendWebRequest(); if (headRequest.isNetworkError || headRequest.isHttpError) { if (OnDownloadErrorHandler != null) OnDownloadErrorHandler(headRequest.error); } else { fileLength = long.Parse(headRequest.GetResponseHeader("Content-Length")); if (downloadType == DownloadType.New) { if (File.Exists(path)) File.Delete(path); } fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); fileCurLength = fileStream.Length; progress = (float)fileCurLength / fileLength; if (OnDownloadBeginHandler != null) OnDownloadBeginHandler(); //当前请求下载的资源未完成下载,继续下载 if (fileCurLength < fileLength) { fileStream.Seek(fileCurLength, SeekOrigin.Begin); webRequest = UnityWebRequest.Get(url); webRequest.SetRequestHeader("Range", "bytes=" + fileCurLength + "-" + fileLength); webRequest.SendWebRequest(); if (webRequest.isNetworkError || webRequest.isHttpError) { if (OnDownloadErrorHandler != null) OnDownloadErrorHandler(webRequest.error); } else { var index = 0; while (fileCurLength < fileLength) { //停止请求下载 if (IsStop) { Clear(); break; } yield return null; var buff = webRequest.downloadHandler.data; if (buff != null) { var length = buff.Length - index; fileStream.Write(buff, index, length); index += length; fileCurLength += length; progress = (float)fileCurLength / fileLength; } } } } else { progress = 1f; if (OnDownloadEndHandler != null) OnDownloadEndHandler(); } Clear(); } } public override void Clear() { if (fileStream != null) { fileStream.Flush(); fileStream.Close(); fileStream = null; } if (webRequest != null) { webRequest.Abort(); webRequest = null; } if (headRequest != null) { headRequest.Abort(); headRequest = null; } CoroutineManager.Instance.StopGlobalCoroutine(SendUnityWebHttpRequest()); } }}

下篇文章 https://blog.csdn.net/wlqchengzhangji/article/details/118705159

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。