当前位置:   article > 正文

[游戏开发][Unity]UnityWebRequest使用大全_unity downloadhandler

unity downloadhandler

首先记录个小问题

使用new UnityWebRequest的方式,最终的downloadHandler是个null

使用UnityWebRequest.Get的方式,最终的downloadHandler会是DownloadHandlerBuffer

加载本地文件,记得路径前要加"file://"

  1. string path = "file://" + Application.streamingAssetsPath + "/Test.txt";
  2. using (UnityWebRequest www = UnityWebRequest.Get(filePath))
  3. {
  4. yield return www.SendWebRequest();
  5. if (www.isHttpError || www.isNetworkError)
  6. {
  7. // 出现Error
  8. Debug.Log("load Config Error: " + filePath + " Error:" + www.error);
  9. }
  10. else
  11. {
  12. //加载的二进制数据
  13. //www.downloadHandler.data;
  14. }
  15. }

从网站或本地下载内容,包括文本或二进制数据

  1. IEnumerator downloadfile(string path)
  2. {
  3. UnityWebRequest uwr = UnityWebRequest.Get(path);
  4. yield return uwr.SendWebRequest();
  5. if (uwr.result == UnityWebRequest.Result.Success)
  6.     {
  7.         //加载文本
  8.         Debug.Log("Success " + uwr.downloadHandler.text);
  9.         //加载二进制数据
  10.         //Debug.Log("Success " + uwr.downloadHandler.data);
  11.     }
  12. else
  13. Debug.LogError(uwr.error);
  14. }

从网站或本地下载贴图

  1. IEnumerator downloadfile(string path)
  2. {
  3. UnityWebRequest uwr = UnityWebRequest.Get(path);
  4. yield return uwr.SendWebRequest();
  5. if (uwr.result == UnityWebRequest.Result.Success)
  6. {
  7.         //加载贴图
  8.     Texture2D tex2D = DownloadHandlerTexture.GetContent(uwr);
  9. }
  10. else
  11. Debug.LogError(uwr.error);
  12. }

从网站或本地下载Assetbundle

  1. IEnumerator downloadfile(string path)
  2. {
  3. UnityWebRequest uwr = UnityWebRequest.Get(path);
  4. yield return uwr.SendWebRequest();
  5. if (uwr.result == UnityWebRequest.Result.Success)
  6. {
  7. //加载AB
  8. AssetBundle ab = DownloadHandlerAssetBundle.GetContent(uwr)
  9. }
  10. else
  11. Debug.LogError(uwr.error);
  12. }

将表单发送到 http 服务器 (post)

  1. IEnumerator downloadfile(string path)
  2. {
  3. List<IMultipartFormSection> form = new List<IMultipartFormSection>();
  4. form.Add(new MultipartFormDataSection("fileld1=A&field2=B"));
  5. form.Add(new MultipartFormFileSection("data", "file.txt"));
  6. UnityWebRequest uwr = UnityWebRequest.Post(path,form);
  7. yield return uwr.SendWebRequest();
  8. if (uwr.result == UnityWebRequest.Result.Success)
  9. Debug.Log("Success " + uwr.downloadHandler.text);
  10. else
  11. Debug.LogError(uwr.error);
  12. }

将原始数据上传到 http 服务器 (put)

  1. IEnumerator Upload()
  2. {
  3. byte[] myData = System.Text.Encoding.UTF8.GetBytes("Chinar的测试数据");
  4. using (UnityWebRequest uwr = UnityWebRequest.Put("http://www.baidu.com", myData))
  5. {
  6. yield return uwr.SendWebRequest();
  7.         if (uwr.result == UnityWebRequest.Result.Success)
  8. Debug.Log("Success " + uwr.downloadHandler.text);
  9. else
  10. Debug.LogError(uwr.error);
  11. }
  12. }

断点续传

记录已经下载到的本地文件大小,向资源服务器发送请求时,通过请求头实现拿到剩下需要下载的内容,然后接着下载

确保对同一个资源文件的下载操作,就不存在资源会下载错误的情况,如果你在断点续传的阶段发现资源服务器上的资源已经更新,那就得删除之前下载的文件然后重新下载。

  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. using UnityEngine.UI;
  7. public class ChinarBreakpointRenewal : MonoBehaviour
  8. {
  9. private bool _isStop; //是否暂停
  10. public Slider ProgressBar; //进度条
  11. public Text SliderValue; //滑动条值
  12. public Button startBtn; //开始按钮
  13. public Button pauseBtn; //暂停按钮
  14. string Url = "https://downsc.chinaz.net/Files/DownLoad/sound1/201808/10447.wav";
  15. /// <summary>
  16. /// 初始化UI界面及给按钮绑定方法
  17. /// </summary>
  18. void Start()
  19. {
  20. //初始化进度条和文本框
  21. ProgressBar.value = 0;
  22. SliderValue.text = "0.0%";
  23. //开始、暂停按钮事件监听
  24. startBtn.onClick.AddListener(OnClickStartDownload);
  25. pauseBtn.onClick.AddListener(OnClickStop);
  26. }
  27. //开始下载按钮监听事件
  28. public void OnClickStartDownload()
  29. {
  30. //开启协程 *注意真机上要用Application.persistentDataPath路径*
  31. StartCoroutine(DownloadFile(Url, Application.streamingAssetsPath + "/MP4/test.mp4", CallBack));
  32. }
  33. /// <summary>
  34. /// 协程:下载文件
  35. /// </summary>
  36. /// <param name="url">请求的Web地址</param>
  37. /// <param name="filePath">文件保存路径</param>
  38. /// <param name="callBack">下载完成的回调函数</param>
  39. /// <returns></returns>
  40. IEnumerator DownloadFile(string url, string filePath, Action callBack)
  41. {
  42. UnityWebRequest huwr = UnityWebRequest.Head(url); //使用Head方法可以获取到文件的全部长度
  43. yield return huwr.SendWebRequest();//发送信息请求
  44. //判断请求或系统是否出错
  45. if (huwr.isNetworkError || huwr.isHttpError)
  46. {
  47. Debug.Log(huwr.error); //出现错误 输出错误信息
  48. }
  49. else
  50. {
  51. long totalLength = long.Parse(huwr.GetResponseHeader("Content-Length")); //首先拿到文件的全部长度
  52. string dirPath = Path.GetDirectoryName(filePath);//获取文件的上一级目录
  53. if (!Directory.Exists(dirPath)) //判断路径是否存在
  54. {
  55. Directory.CreateDirectory(dirPath);//不存在创建
  56. }
  57. /*作用:创建一个文件流,指定路径为filePath,模式为打开或创建,访问为写入
  58. * 使用using(){}方法原因: 当同一个cs引用了不同的命名空间,但这些命名控件都包括了一个相同名字的类型的时候,可以使用using关键字来创建别名,这样会使代码更简洁。注意:并不是说两个名字重复,给其中一个用了别名,另外一个就不需要用别名了,如果两个都要使用,则两个都需要用using来定义别名的
  59. * using(类){} 括号中的类必须是继承了IDisposable接口才能使用否则报错
  60. * 这里没有出现不同命名空间出现相同名字的类属性可以不用using(){}
  61. */
  62. using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))
  63. {
  64. long nowFileLength = fs.Length; //当前文件长度,断点前已经下载的文件长度。
  65. Debug.Log(fs.Length);
  66. //判断当前文件是否小于要下载文件的长度,即文件是否下载完成
  67. if (nowFileLength < totalLength)
  68. {
  69. Debug.Log("还没下载完成");
  70. /*使用Seek方法 可以随机读写文件
  71. * Seek() ----------有两个参数 第一参数规定文件指针以字节为单位移动的距离。第二个参数规定开始计算的位置
  72. * 第二个参数SeekOrigin 有三个值:Begin Current End
  73. * fs.Seek(8,SeekOrigin.Begin);表示 将文件指针从开头位置移动到文件的第8个字节
  74. * fs.Seek(8,SeekOrigin.Current);表示 将文件指针从当前位置移动到文件的第8个字节
  75. * fs.Seek(8,SeekOrigin.End);表示 将文件指针从最后位置移动到文件的第8个字节
  76. */
  77. fs.Seek(nowFileLength, SeekOrigin.Begin); //从开头位置,移动到当前已下载的子节位置
  78. UnityWebRequest uwr = UnityWebRequest.Get(url); //创建UnityWebRequest对象,将Url传入
  79. uwr.SetRequestHeader("Range", "bytes=" + nowFileLength + "-" + totalLength);//修改请求头从n-m之间
  80. uwr.SendWebRequest(); //开始请求
  81. if (uwr.isNetworkError || uwr.isHttpError) //如果出错
  82. {
  83. Debug.Log(uwr.error); //输出 错误信息
  84. }
  85. else
  86. {
  87. long index = 0; //从该索引处继续下载
  88. while (nowFileLength < totalLength) //只要下载没有完成,一直执行此循环
  89. {
  90. if (_isStop) break;//如果停止跳出循环
  91. yield return null;
  92. byte[] data = uwr.downloadHandler.data;
  93. if (data != null)
  94. {
  95. long length = data.Length - index;
  96. fs.Write(data, (int)index, (int)length); //写入文件
  97. index += length;
  98. nowFileLength += length;
  99. ProgressBar.value = (float)nowFileLength / totalLength;
  100. SliderValue.text = Math.Floor((float)nowFileLength / totalLength * 100) + "%";
  101. if (nowFileLength >= totalLength) //如果下载完成了
  102. {
  103. ProgressBar.value = 1; //改变Slider的值
  104. SliderValue.text = 100 + "%";
  105. /*这句话的作用是:如果callBack方法不为空则执行Invoke
  106. * 注意:
  107. * 1.这里的Invoke可不是Unity的Invoke延迟调用的用法,参考文章:https://blog.csdn.net/liujiejieliu1234/article/details/45312141 从文章中我们可以看到,C#中的Invoke是为了防止winform中子主线程刚开始创建对象时,子线程与主线程并发修改主线程尚未创建的对象属性。
  108. * 因为unity这里只有主线程没有用到子线程可以直接写callBack();
  109. */
  110. callBack?.Invoke();
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// 下载完成后的回调函数
  122. /// </summary>
  123. void CallBack()
  124. {
  125. Debug.Log("下载完成");
  126. }
  127. /// <summary>
  128. /// 暂停下载
  129. /// </summary>
  130. public void OnClickStop()
  131. {
  132. if (_isStop)
  133. {
  134. pauseBtn.GetComponentInChildren<Text>().text = "暂停下载";
  135. Debug.Log("继续下载");
  136. _isStop = !_isStop;
  137. OnClickStartDownload();
  138. }
  139. else
  140. {
  141. pauseBtn.GetComponentInChildren<Text>().text = "继续下载";
  142. Debug.Log("暂停下载");
  143. _isStop = !_isStop;
  144. }
  145. }
  146. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/95715
推荐阅读
相关标签
  

闽ICP备14008679号