AssetLoadFromBundleAsync.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using XGame.Framework.Asyncs;
  2. using System;
  3. using UnityEngine;
  4. namespace XGame.Framework.Asset
  5. {
  6. internal class AssetLoadFromBundleAsync : Async, IAssetAsync
  7. {
  8. //public AssetBundle Bundle { get; set; }
  9. private AssetBundleRequest request;
  10. ///// <summary>
  11. ///// 此处Path无用
  12. ///// </summary>
  13. //public string Path { get; internal set; }
  14. public string AddressableName { get; internal set; }
  15. /// <summary>
  16. /// asset所在Bundle的Path
  17. /// </summary>
  18. internal string BundleName { get; set; }
  19. internal bool IsSprite { get; set; }
  20. /// <summary>
  21. /// async的Start标记
  22. /// </summary>
  23. private bool isStart;
  24. /// <summary>
  25. /// asyncGroup完成标记
  26. /// </summary>
  27. private bool groupCompleted;
  28. /// <summary>
  29. /// asset所依赖bundle的AsyncGroup
  30. /// </summary>
  31. private AsyncGroup bundleAsyncGroup;
  32. internal AsyncGroup BundleAsyncGroup
  33. {
  34. get => bundleAsyncGroup;
  35. set
  36. {
  37. //groupCompleted = false;
  38. bundleAsyncGroup = value;
  39. bundleAsyncGroup?.On(OnGroupCompleted);
  40. }
  41. }
  42. public Func<string, AssetBundle> GetBundleFromCache { set; get; }
  43. public int AssetHash { get; set; }
  44. public override float Progress
  45. {
  46. get
  47. {
  48. float progress = BundleAsyncGroup?.Progress ?? 0 + request?.progress ?? 0;
  49. return progress / 2;
  50. }
  51. protected set => base.Progress = value;
  52. }
  53. public object GetResult()
  54. {
  55. object result = null;
  56. if (request != null)
  57. result = request.asset;
  58. Clear();
  59. return result;
  60. }
  61. public void Start()
  62. {
  63. isStart = true;
  64. if (bundleAsyncGroup == null || groupCompleted)
  65. {
  66. StartLoadAsset();
  67. }
  68. //if (Bundle != null)
  69. //{
  70. // string assetName = System.IO.Path.GetFileNameWithoutExtension(Path);
  71. // request = Bundle.LoadAssetAsync(assetName);
  72. // request.completed += OnAssetCompleted;
  73. //}
  74. }
  75. void StartLoadAsset()
  76. {
  77. var bundle = GetBundleFromCache?.Invoke(BundleName);
  78. if (bundle != null)
  79. {
  80. //string assetName = Path.ToLower();// System.IO.Path.GetFileNameWithoutExtension(Path);
  81. request = IsSprite ? bundle.LoadAssetAsync(AddressableName, typeof(Sprite)) : bundle.LoadAssetAsync(AddressableName);
  82. request.completed += OnAssetCompleted;
  83. }
  84. else
  85. {
  86. AssetsLog.Error($"AssetBundle load completed. But it's invalid. Bundle:{BundleName} Asset:{AddressableName}");
  87. AssetHash = 0;
  88. Completed();
  89. }
  90. }
  91. void OnGroupCompleted(IAsync async)
  92. {
  93. groupCompleted = true;
  94. if (isStart)
  95. {
  96. StartLoadAsset();
  97. }
  98. }
  99. void OnAssetCompleted(AsyncOperation async)
  100. {
  101. if (!async.isDone || request.asset == null)
  102. AssetsLog.Warn("{0} is not a valid asset.", AddressableName);
  103. AssetHash = request.asset?.GetInstanceID() ?? 0;
  104. Completed();
  105. }
  106. void Clear()
  107. {
  108. isStart = false;
  109. groupCompleted = false;
  110. request = null;
  111. bundleAsyncGroup = null;
  112. }
  113. }
  114. }