UnityWebDataRequester.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine.Networking;
  5. using UnityEngine;
  6. namespace YooAsset
  7. {
  8. internal class UnityWebDataRequester : UnityWebRequesterBase
  9. {
  10. /// <summary>
  11. /// 发送GET请求
  12. /// </summary>
  13. public void SendRequest(string url, int timeout = 60)
  14. {
  15. if (_webRequest == null)
  16. {
  17. URL = url;
  18. ResetTimeout(timeout);
  19. _webRequest = DownloadHelper.NewRequest(URL);
  20. DownloadHandlerBuffer handler = new DownloadHandlerBuffer();
  21. _webRequest.downloadHandler = handler;
  22. _webRequest.disposeDownloadHandlerOnDispose = true;
  23. _operationHandle = _webRequest.SendWebRequest();
  24. }
  25. }
  26. /// <summary>
  27. /// 获取下载的字节数据
  28. /// </summary>
  29. public byte[] GetData()
  30. {
  31. if (_webRequest != null && IsDone())
  32. return _webRequest.downloadHandler.data;
  33. else
  34. return null;
  35. }
  36. /// <summary>
  37. /// 获取下载的文本数据
  38. /// </summary>
  39. public string GetText()
  40. {
  41. if (_webRequest != null && IsDone())
  42. return _webRequest.downloadHandler.text;
  43. else
  44. return null;
  45. }
  46. }
  47. }