com.alicizax.unity.tuyoogam.../Runtime/ResourceManager/Operation/UnloadAllAssetsOperation.cs

132 lines
3.9 KiB
C#
Raw Normal View History

2025-01-09 11:31:04 +08:00
using System;
using UnityEngine;
namespace YooAsset
{
2025-02-28 16:11:01 +08:00
public sealed class UnloadAllAssetsOptions
{
/// <summary>
/// 释放所有资源句柄,防止卸载过程中触发完成回调!
/// </summary>
public bool ReleaseAllHandles = false;
/// <summary>
/// 卸载过程中锁定加载操作,防止新的任务请求!
/// </summary>
public bool LockLoadOperation = false;
}
2025-01-09 11:31:04 +08:00
public sealed class UnloadAllAssetsOperation : AsyncOperationBase
{
private enum ESteps
{
None,
2025-02-28 16:11:01 +08:00
CheckOptions,
ReleaseAll,
2025-01-09 11:31:04 +08:00
AbortDownload,
CheckLoading,
2025-02-28 16:11:01 +08:00
DestroyAll,
2025-01-09 11:31:04 +08:00
Done,
}
private readonly ResourceManager _resManager;
2025-02-28 16:11:01 +08:00
private readonly UnloadAllAssetsOptions _options;
2025-01-09 11:31:04 +08:00
private ESteps _steps = ESteps.None;
2025-02-28 16:11:01 +08:00
internal UnloadAllAssetsOperation(ResourceManager resourceManager, UnloadAllAssetsOptions options)
2025-01-09 11:31:04 +08:00
{
_resManager = resourceManager;
2025-02-28 16:11:01 +08:00
_options = options;
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
internal override void InternalStart()
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
_steps = ESteps.CheckOptions;
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
internal override void InternalUpdate()
2025-01-09 11:31:04 +08:00
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
2025-02-28 16:11:01 +08:00
if (_steps == ESteps.CheckOptions)
{
if (_options == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"{nameof(UnloadAllAssetsOptions)} is null.";
return;
}
// 设置锁定状态
if (_options.LockLoadOperation)
_resManager.LockLoadOperation = true;
_steps = ESteps.ReleaseAll;
}
if (_steps == ESteps.ReleaseAll)
{
// 清空所有场景句柄
_resManager.SceneHandles.Clear();
// 释放所有资源句柄
if (_options.ReleaseAllHandles)
{
foreach (var provider in _resManager.ProviderDic.Values)
{
provider.ReleaseAllHandles();
}
}
_steps = ESteps.AbortDownload;
}
2025-01-09 11:31:04 +08:00
if (_steps == ESteps.AbortDownload)
{
// 注意:终止所有下载任务
2025-02-28 16:11:01 +08:00
foreach (var loader in _resManager.LoaderDic.Values)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
loader.AbortOperation();
2025-01-09 11:31:04 +08:00
}
_steps = ESteps.CheckLoading;
}
if (_steps == ESteps.CheckLoading)
{
// 注意:等待所有任务完成
2025-02-28 16:11:01 +08:00
foreach (var provider in _resManager.ProviderDic.Values)
2025-01-09 11:31:04 +08:00
{
if (provider.IsDone == false)
return;
}
2025-02-28 16:11:01 +08:00
_steps = ESteps.DestroyAll;
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
if (_steps == ESteps.DestroyAll)
2025-01-09 11:31:04 +08:00
{
// 强制销毁资源提供者
2025-02-28 16:11:01 +08:00
foreach (var provider in _resManager.ProviderDic.Values)
2025-01-09 11:31:04 +08:00
{
provider.DestroyProvider();
}
// 强制销毁文件加载器
2025-02-28 16:11:01 +08:00
foreach (var loader in _resManager.LoaderDic.Values)
2025-01-09 11:31:04 +08:00
{
loader.DestroyLoader();
}
// 清空数据
2025-02-28 16:11:01 +08:00
_resManager.ProviderDic.Clear();
_resManager.LoaderDic.Clear();
_resManager.LockLoadOperation = false;
2025-01-09 11:31:04 +08:00
// 注意:调用底层接口释放所有资源
Resources.UnloadUnusedAssets();
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
}
}
}