UnloadAllAssetsOperation.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using UnityEngine;
  3. namespace YooAsset
  4. {
  5. public sealed class UnloadAllAssetsOptions
  6. {
  7. /// <summary>
  8. /// 释放所有资源句柄,防止卸载过程中触发完成回调!
  9. /// </summary>
  10. public bool ReleaseAllHandles = false;
  11. /// <summary>
  12. /// 卸载过程中锁定加载操作,防止新的任务请求!
  13. /// </summary>
  14. public bool LockLoadOperation = false;
  15. }
  16. public sealed class UnloadAllAssetsOperation : AsyncOperationBase
  17. {
  18. private enum ESteps
  19. {
  20. None,
  21. CheckOptions,
  22. ReleaseAll,
  23. AbortDownload,
  24. CheckLoading,
  25. DestroyAll,
  26. Done,
  27. }
  28. private readonly ResourceManager _resManager;
  29. private readonly UnloadAllAssetsOptions _options;
  30. private ESteps _steps = ESteps.None;
  31. internal UnloadAllAssetsOperation(ResourceManager resourceManager, UnloadAllAssetsOptions options)
  32. {
  33. _resManager = resourceManager;
  34. _options = options;
  35. }
  36. internal override void InternalStart()
  37. {
  38. _steps = ESteps.CheckOptions;
  39. }
  40. internal override void InternalUpdate()
  41. {
  42. if (_steps == ESteps.None || _steps == ESteps.Done)
  43. return;
  44. if (_steps == ESteps.CheckOptions)
  45. {
  46. if (_options == null)
  47. {
  48. _steps = ESteps.Done;
  49. Status = EOperationStatus.Failed;
  50. Error = $"{nameof(UnloadAllAssetsOptions)} is null.";
  51. return;
  52. }
  53. // 设置锁定状态
  54. if (_options.LockLoadOperation)
  55. _resManager.LockLoadOperation = true;
  56. _steps = ESteps.ReleaseAll;
  57. }
  58. if (_steps == ESteps.ReleaseAll)
  59. {
  60. // 清空所有场景句柄
  61. _resManager.SceneHandles.Clear();
  62. // 释放所有资源句柄
  63. if (_options.ReleaseAllHandles)
  64. {
  65. foreach (var provider in _resManager.ProviderDic.Values)
  66. {
  67. provider.ReleaseAllHandles();
  68. }
  69. }
  70. _steps = ESteps.AbortDownload;
  71. }
  72. if (_steps == ESteps.AbortDownload)
  73. {
  74. // 注意:终止所有下载任务
  75. foreach (var loader in _resManager.LoaderDic.Values)
  76. {
  77. loader.AbortOperation();
  78. }
  79. _steps = ESteps.CheckLoading;
  80. }
  81. if (_steps == ESteps.CheckLoading)
  82. {
  83. // 注意:等待所有任务完成
  84. foreach (var provider in _resManager.ProviderDic.Values)
  85. {
  86. if (provider.IsDone == false)
  87. return;
  88. }
  89. _steps = ESteps.DestroyAll;
  90. }
  91. if (_steps == ESteps.DestroyAll)
  92. {
  93. // 强制销毁资源提供者
  94. foreach (var provider in _resManager.ProviderDic.Values)
  95. {
  96. provider.DestroyProvider();
  97. }
  98. // 强制销毁文件加载器
  99. foreach (var loader in _resManager.LoaderDic.Values)
  100. {
  101. loader.DestroyLoader();
  102. }
  103. // 清空数据
  104. _resManager.ProviderDic.Clear();
  105. _resManager.LoaderDic.Clear();
  106. _resManager.LockLoadOperation = false;
  107. // 注意:调用底层接口释放所有资源
  108. Resources.UnloadUnusedAssets();
  109. _steps = ESteps.Done;
  110. Status = EOperationStatus.Succeed;
  111. }
  112. }
  113. }
  114. }