WebRequestAssetBundleAsync.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. using XGame.Framework.Asyncs;
  4. namespace XGame.Framework.Asset
  5. {
  6. internal class WebRequestAssetBundleAsync : Async, IAssetAsync
  7. {
  8. private UnityWebRequest request;
  9. private AsyncOperation operation;
  10. public string Path { get; internal set; }
  11. public uint Offset { get; internal set; }
  12. /// <summary>
  13. /// 此处先存bundleName
  14. /// </summary>
  15. public string AddressableName { get; internal set; }
  16. public override float Progress
  17. {
  18. get => operation != null ? operation.progress : 0;
  19. protected set => base.Progress = value;
  20. }
  21. public void Start()
  22. {
  23. //Log.Warn($"AssetBundleCreateRequestAsync Start. Path:{Path}");
  24. request = UnityWebRequestAssetBundle.GetAssetBundle(Path);
  25. operation = request.SendWebRequest();
  26. operation.completed += OnCompleted;
  27. }
  28. void OnCompleted(AsyncOperation async)
  29. {
  30. if (!async.isDone)
  31. {
  32. AssetsLog.Warn($"Assetbundle operation error. name:{AddressableName} Path:{Path}");
  33. }
  34. else if (request == null || !request.isDone)
  35. {
  36. AssetsLog.Warn($"Assetbundle request error. name:{AddressableName} Path:{Path}");
  37. }
  38. Completed();
  39. }
  40. public object GetResult()
  41. {
  42. object result = null;
  43. if (request != null)
  44. {
  45. //Log.Warn($"AssetBundleCreateRequestAsync Get bundle:{request.assetBundle? .GetInstanceID()??0} Path:{Path}");
  46. result = DownloadHandlerAssetBundle.GetContent(request);
  47. }
  48. if (result == null)
  49. {
  50. AssetsLog.Warn($"Assetbundle is null. name:{AddressableName} Path:{Path} request:{request != null}");
  51. }
  52. Clear();
  53. return result;
  54. }
  55. void Clear()
  56. {
  57. request = null;
  58. operation = null;
  59. }
  60. }
  61. }