AssetLoadAsync.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using XGame.Framework.Asyncs;
  2. using System;
  3. namespace XGame.Framework.Asset
  4. {
  5. /// <summary>
  6. /// 该类也继承IAssetPreloadAsync,少创建一个对象
  7. /// 业务禁止将IAssetPreloadAsync强转为IAssetLoadAsync<TResult>
  8. /// </summary>
  9. /// <typeparam name="TResult"></typeparam>
  10. internal sealed class AssetLoadAsync<TResult> : IAssetLoadAsync<TResult>, IAssetEventListenerSetter, IAssetPreloadAsync
  11. {
  12. private AsyncListenerOperation _operation;
  13. private IAssetAsync _assetAsync;
  14. private IAssetCacheHandle _cacheHandle;
  15. private IAssetEventListener _eventListener;
  16. IAssetEventListener IAssetEventListenerSetter.AssetEventListener { set => _eventListener = value; }
  17. internal AssetLoadAsync(string addressableName, IAssetAsync assetAsync, IAssetCacheHandle handle)
  18. {
  19. AddressableName = addressableName;
  20. _assetAsync = assetAsync;
  21. _cacheHandle = handle;
  22. if (assetAsync != null && assetAsync.IsCompleted == false)
  23. {
  24. assetAsync.On(OnAsyncCompleted);
  25. }
  26. }
  27. private void OnAsyncCompleted(IAsync aAsync)
  28. {
  29. _operation.OnCompleted(this);
  30. PreRelease();
  31. }
  32. private void PreRelease()
  33. {
  34. if (_operation.HaveListener)
  35. return;
  36. RemoveAll();
  37. }
  38. #region 接口实现
  39. object IAsync.State
  40. {
  41. set
  42. {
  43. var assetAsync = _assetAsync;
  44. if (assetAsync != null)
  45. assetAsync.State = value;
  46. }
  47. get => _assetAsync?.State;
  48. }
  49. public Exception Exception => _assetAsync?.Exception;
  50. public bool IsCompleted => _assetAsync?.IsCompleted ?? true;
  51. /// <summary>
  52. /// 返回加载进度
  53. /// async为空时默认加载完成
  54. /// </summary>
  55. public float Progress => _assetAsync?.Progress ?? 1;
  56. public string AddressableName { get; private set; }
  57. public IAsync On(OnAsyncCompleted action)
  58. {
  59. if (action != null)
  60. {
  61. if (IsCompleted)
  62. {
  63. action.SaveInvoke(this);
  64. PreRelease();
  65. }
  66. else
  67. {
  68. _operation.On(action);
  69. }
  70. }
  71. return this;
  72. }
  73. public IAsync On(IAsyncListener listener, int order = 0)
  74. {
  75. if (listener == null) return this;
  76. if (IsCompleted)
  77. {
  78. listener.SaveInvoke(this);
  79. PreRelease();
  80. return this;
  81. }
  82. _operation.On(listener, order);
  83. return this;
  84. }
  85. public IAsync RemoveOn(OnAsyncCompleted action)
  86. {
  87. _operation.RemoveOn(action);
  88. return this;
  89. }
  90. public IAsync RemoveOn(IAsyncListener listener)
  91. {
  92. _operation.RemoveOn(listener);
  93. return this;
  94. }
  95. public IAsync RemoveAll()
  96. {
  97. //Log.Debug($"AssetLoadAsync RemoveAll Name:{AddressableName}");
  98. if (!IsCompleted)
  99. _assetAsync?.RemoveOn(OnAsyncCompleted);
  100. _assetAsync = null;
  101. _cacheHandle?.ReleaseAsync(this);
  102. _cacheHandle = null;
  103. _eventListener?.ReleaseAsync(this);
  104. _eventListener = null;
  105. _operation.Clear();
  106. return this;
  107. }
  108. public TResult Result
  109. {
  110. get
  111. {
  112. TResult result = default;
  113. var addressableName = AddressableName;
  114. if (_cacheHandle != null)
  115. result = _cacheHandle.GetAssetFromCache(addressableName).Convert<TResult>(addressableName);
  116. //先 Convert, 保证getResultCallback和最终返回的是同一个对象
  117. if (result != null)
  118. {
  119. _eventListener?.OnGetResult(addressableName, result);
  120. }
  121. return result;
  122. }
  123. }
  124. #endregion
  125. #if UNITY_EDITOR
  126. public override string ToString()
  127. {
  128. return $"AssetLoadAsync Type:{typeof(TResult)} Name:{AddressableName} IsCompleted:{IsCompleted}";
  129. }
  130. #endif
  131. }
  132. }