WXDownload.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. using Utility;
  6. #if UNITY_WEBGL || UNITY_EDITOR
  7. using WeChatWASM;
  8. #endif
  9. public class WXDownload : Singleton<WXDownload>
  10. {
  11. public void DownLoad(string url, string fileName, System.Action<bool> finish)
  12. {
  13. #if UNITY_WEBGL || UNITY_EDITOR
  14. DownloadFileOption fileOption = new DownloadFileOption();
  15. fileOption.url = url;
  16. fileOption.filePath = GetPluginCachePath() + "/" + fileName;
  17. fileOption.fail = delegate(GeneralCallbackResult result)
  18. {
  19. Debug.LogError(url + "下载失败" + result.errMsg);
  20. finish?.Invoke(false);
  21. };
  22. fileOption.success = delegate(DownloadFileSuccessCallbackResult result)
  23. {
  24. finish?.Invoke(true);
  25. // Debug.Log(url + "下载成功");
  26. };
  27. WX.DownloadFile(fileOption);
  28. #elif UNITY_ANDROID
  29. UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
  30. unityWebRequest.downloadHandler = new DownloadHandlerBuffer();
  31. var unityWebRequestAsyncOperation = unityWebRequest.SendWebRequest();
  32. unityWebRequestAsyncOperation.completed += operation =>
  33. {
  34. if (unityWebRequest.result == UnityWebRequest.Result.Success)
  35. {
  36. finish?.Invoke(true);
  37. }
  38. else
  39. {
  40. Debug.LogError(url + "下载失败" + unityWebRequest.result);
  41. finish?.Invoke(false);
  42. }
  43. };
  44. #endif
  45. }
  46. public string GetCachePath(string url)
  47. {
  48. #if UNITY_WEBGL || UNITY_EDITOR
  49. return WX.GetCachePath(url);
  50. #elif UNITY_ANDROID
  51. return url;
  52. #endif
  53. }
  54. public string GetPluginCachePath()
  55. {
  56. #if UNITY_WEBGL || UNITY_EDITOR
  57. return WX.PluginCachePath;
  58. #elif UNITY_ANDROID
  59. return Application.persistentDataPath;
  60. #endif
  61. }
  62. }