AssetBundleWebLoader.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.Networking;
  7. namespace YooAsset
  8. {
  9. /// <summary>
  10. /// WebGL平台加载器
  11. /// </summary>
  12. internal sealed class AssetBundleWebLoader : BundleLoaderBase
  13. {
  14. private enum ESteps
  15. {
  16. None = 0,
  17. LoadWebSiteFile,
  18. LoadRemoteFile,
  19. CheckLoadFile,
  20. Done,
  21. }
  22. private ESteps _steps = ESteps.None;
  23. private DownloaderBase _downloader;
  24. public AssetBundleWebLoader(ResourceManager impl, BundleInfo bundleInfo) : base(impl, bundleInfo)
  25. {
  26. }
  27. /// <summary>
  28. /// 轮询更新
  29. /// </summary>
  30. public override void Update()
  31. {
  32. if (_steps == ESteps.Done)
  33. return;
  34. if (_steps == ESteps.None)
  35. {
  36. if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
  37. {
  38. _steps = ESteps.LoadRemoteFile;
  39. FileLoadPath = string.Empty;
  40. }
  41. else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
  42. {
  43. _steps = ESteps.LoadWebSiteFile;
  44. FileLoadPath = string.Empty;
  45. }
  46. else
  47. {
  48. throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
  49. }
  50. }
  51. // 1. 跨域获取资源包
  52. if (_steps == ESteps.LoadRemoteFile)
  53. {
  54. _downloader = MainBundleInfo.CreateDownloader(int.MaxValue);
  55. _downloader.SendRequest(true);
  56. _steps = ESteps.CheckLoadFile;
  57. }
  58. // 2. 从站点获取资源包
  59. if (_steps == ESteps.LoadWebSiteFile)
  60. {
  61. _downloader = MainBundleInfo.CreateUnpacker(int.MaxValue);
  62. _downloader.SendRequest(true);
  63. _steps = ESteps.CheckLoadFile;
  64. }
  65. // 3. 检测加载结果
  66. if (_steps == ESteps.CheckLoadFile)
  67. {
  68. DownloadProgress = _downloader.DownloadProgress;
  69. DownloadedBytes = _downloader.DownloadedBytes;
  70. if (_downloader.IsDone() == false)
  71. return;
  72. CacheBundle = _downloader.GetAssetBundle();
  73. if (CacheBundle == null)
  74. {
  75. _steps = ESteps.Done;
  76. Status = EStatus.Failed;
  77. LastError = $"AssetBundle file is invalid : {MainBundleInfo.Bundle.BundleName}";
  78. YooLogger.Error(LastError);
  79. }
  80. else
  81. {
  82. _steps = ESteps.Done;
  83. Status = EStatus.Succeed;
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// 主线程等待异步操作完毕
  89. /// </summary>
  90. public override void WaitForAsyncComplete()
  91. {
  92. if (IsDone() == false)
  93. {
  94. Status = EStatus.Failed;
  95. LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !";
  96. YooLogger.Error(LastError);
  97. }
  98. }
  99. }
  100. }