BundleLoaderBase.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace YooAsset
  6. {
  7. internal abstract class BundleLoaderBase
  8. {
  9. public enum EStatus
  10. {
  11. None = 0,
  12. Succeed,
  13. Failed
  14. }
  15. /// <summary>
  16. /// 所属资源系统
  17. /// </summary>
  18. public ResourceManager Impl { private set; get; }
  19. /// <summary>
  20. /// 资源包文件信息
  21. /// </summary>
  22. public BundleInfo MainBundleInfo { private set; get; }
  23. /// <summary>
  24. /// 引用计数
  25. /// </summary>
  26. public int RefCount { private set; get; }
  27. /// <summary>
  28. /// 加载状态
  29. /// </summary>
  30. public EStatus Status { protected set; get; }
  31. /// <summary>
  32. /// 最近的错误信息
  33. /// </summary>
  34. public string LastError { protected set; get; }
  35. /// <summary>
  36. /// 是否已经销毁
  37. /// </summary>
  38. public bool IsDestroyed { private set; get; } = false;
  39. private readonly List<ProviderBase> _providers = new List<ProviderBase>(100);
  40. private readonly List<ProviderBase> _removeList = new List<ProviderBase>(100);
  41. protected bool IsForceDestroyComplete { private set; get; } = false;
  42. internal AssetBundle CacheBundle { set; get; }
  43. internal string FileLoadPath { set; get; }
  44. internal float DownloadProgress { set; get; }
  45. internal ulong DownloadedBytes { set; get; }
  46. public BundleLoaderBase(ResourceManager impl, BundleInfo bundleInfo)
  47. {
  48. Impl = impl;
  49. MainBundleInfo = bundleInfo;
  50. RefCount = 0;
  51. Status = EStatus.None;
  52. }
  53. /// <summary>
  54. /// 引用(引用计数递加)
  55. /// </summary>
  56. public void Reference()
  57. {
  58. RefCount++;
  59. }
  60. /// <summary>
  61. /// 释放(引用计数递减)
  62. /// </summary>
  63. public void Release()
  64. {
  65. RefCount--;
  66. }
  67. /// <summary>
  68. /// 是否完毕(无论成功或失败)
  69. /// </summary>
  70. public bool IsDone()
  71. {
  72. return Status == EStatus.Succeed || Status == EStatus.Failed;
  73. }
  74. /// <summary>
  75. /// 是否可以销毁
  76. /// </summary>
  77. public bool CanDestroy()
  78. {
  79. if (IsDone() == false)
  80. return false;
  81. return RefCount <= 0;
  82. }
  83. /// <summary>
  84. /// 添加附属的资源提供者
  85. /// </summary>
  86. public void AddProvider(ProviderBase provider)
  87. {
  88. if (_providers.Contains(provider) == false)
  89. _providers.Add(provider);
  90. }
  91. /// <summary>
  92. /// 尝试销毁资源提供者
  93. /// </summary>
  94. public void TryDestroyProviders()
  95. {
  96. // 获取移除列表
  97. _removeList.Clear();
  98. foreach (var provider in _providers)
  99. {
  100. if (provider.CanDestroy())
  101. {
  102. _removeList.Add(provider);
  103. }
  104. }
  105. // 销毁资源提供者
  106. foreach (var provider in _removeList)
  107. {
  108. _providers.Remove(provider);
  109. provider.Destroy();
  110. }
  111. // 移除资源提供者
  112. if (_removeList.Count > 0)
  113. {
  114. Impl.RemoveBundleProviders(_removeList);
  115. _removeList.Clear();
  116. }
  117. }
  118. /// <summary>
  119. /// 轮询更新
  120. /// </summary>
  121. public abstract void Update();
  122. /// <summary>
  123. /// 销毁
  124. /// </summary>
  125. public virtual void Destroy()
  126. {
  127. IsDestroyed = true;
  128. // Check fatal
  129. if (RefCount > 0)
  130. throw new Exception($"Bundle file loader ref is not zero : {MainBundleInfo.Bundle.BundleName}");
  131. if (IsDone() == false)
  132. throw new Exception($"Bundle file loader is not done : {MainBundleInfo.Bundle.BundleName}");
  133. if (CacheBundle != null)
  134. {
  135. CacheBundle.Unload(true);
  136. CacheBundle = null;
  137. }
  138. }
  139. /// <summary>
  140. /// 强制销毁资源提供者
  141. /// </summary>
  142. public void ForceDestroyComplete()
  143. {
  144. IsForceDestroyComplete = true;
  145. // 注意:主动轮询更新完成同步加载
  146. // 说明:如果正在下载或解压也可以放心销毁。
  147. Update();
  148. }
  149. /// <summary>
  150. /// 主线程等待异步操作完毕
  151. /// </summary>
  152. public abstract void WaitForAsyncComplete();
  153. }
  154. }