DownloadSystemHelper.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEngine.Networking;
  2. using UnityEngine;
  3. namespace YooAsset
  4. {
  5. /// <summary>
  6. /// 自定义下载器的请求委托
  7. /// </summary>
  8. public delegate UnityWebRequest UnityWebRequestDelegate(string url);
  9. internal class DownloadSystemHelper
  10. {
  11. #if UNITY_EDITOR
  12. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  13. private static void OnRuntimeInitialize()
  14. {
  15. UnityWebRequestCreater = null;
  16. }
  17. #endif
  18. public static UnityWebRequestDelegate UnityWebRequestCreater = null;
  19. public static UnityWebRequest NewUnityWebRequestGet(string requestURL)
  20. {
  21. UnityWebRequest webRequest;
  22. if (UnityWebRequestCreater != null)
  23. webRequest = UnityWebRequestCreater.Invoke(requestURL);
  24. else
  25. webRequest = new UnityWebRequest(requestURL, UnityWebRequest.kHttpVerbGET);
  26. return webRequest;
  27. }
  28. /// <summary>
  29. /// 获取WWW加载本地资源的路径
  30. /// </summary>
  31. public static string ConvertToWWWPath(string path)
  32. {
  33. string url;
  34. // 获取对应平台的URL地址
  35. #if UNITY_EDITOR_OSX
  36. url = StringUtility.Format("file://{0}", path);
  37. #elif UNITY_EDITOR
  38. url = StringUtility.Format("file:///{0}", path);
  39. #elif UNITY_WEBGL
  40. url = path;
  41. #elif UNITY_IPHONE
  42. url = StringUtility.Format("file://{0}", path);
  43. #elif UNITY_ANDROID
  44. if (path.StartsWith("jar:file://"))
  45. url = path;
  46. else
  47. url = StringUtility.Format("jar:file://{0}", path);
  48. #elif UNITY_OPENHARMONY
  49. if (UnityEngine.Application.streamingAssetsPath.StartsWith("jar:file://"))
  50. {
  51. if (path.StartsWith("jar:file://"))
  52. url = path;
  53. else
  54. url = StringUtility.Format("jar:file://{0}", path);
  55. }
  56. else
  57. {
  58. if (path.StartsWith("file://"))
  59. url = path;
  60. else
  61. url = StringUtility.Format("file://{0}", path);
  62. }
  63. #elif UNITY_STANDALONE_OSX
  64. url = new System.Uri(path).ToString();
  65. #elif UNITY_STANDALONE
  66. url = StringUtility.Format("file:///{0}", path);
  67. #else
  68. throw new System.NotImplementedException();
  69. #endif
  70. // For some special cases when users have special characters in their devices, url paths can not be identified correctly.
  71. return url.Replace("+", "%2B").Replace("#", "%23").Replace("?", "%3F");
  72. }
  73. /// <summary>
  74. /// 是否请求的本地文件
  75. /// </summary>
  76. public static bool IsRequestLocalFile(string url)
  77. {
  78. //TODO UNITY_STANDALONE_OSX平台目前无法确定
  79. if (url.StartsWith("file:"))
  80. return true;
  81. if (url.StartsWith("jar:file:"))
  82. return true;
  83. return false;
  84. }
  85. }
  86. }