diff --git a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs
index cf05a69..ac39ec5 100644
--- a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs
+++ b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs
@@ -13,6 +13,17 @@ namespace YooAsset
private static long _maxTimeSlice;
private static long _frameTime;
+ ///
+ /// 处理器是否繁忙
+ ///
+ public static bool IsBusy
+ {
+ get
+ {
+ return _watch.ElapsedMilliseconds - _frameTime >= _maxTimeSlice;
+ }
+ }
+
///
/// 初始化异步操作系统
@@ -32,7 +43,7 @@ namespace YooAsset
for (int i = _operations.Count - 1; i >= 0; i--)
{
- if (_watch.ElapsedMilliseconds - _frameTime >= _maxTimeSlice)
+ if (IsBusy)
return;
var operation = _operations[i];
diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs b/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs
new file mode 100644
index 0000000..cebf6a7
--- /dev/null
+++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs
@@ -0,0 +1,111 @@
+using System.Collections;
+using System.Collections.Generic;
+using System.IO;
+using UnityEngine;
+
+namespace YooAsset
+{
+ ///
+ /// 清理未使用的缓存资源操作类
+ ///
+ public abstract class ClearUnusedCacheFilesOperation : AsyncOperationBase
+ {
+ }
+
+ ///
+ /// 编辑器模式
+ ///
+ internal sealed class EditorPlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
+ {
+ internal override void Start()
+ {
+ Status = EOperationStatus.Succeed;
+ }
+ internal override void Update()
+ {
+ }
+ }
+
+ ///
+ /// 离线模式
+ ///
+ internal sealed class OfflinePlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
+ {
+ internal override void Start()
+ {
+ Status = EOperationStatus.Succeed;
+ }
+ internal override void Update()
+ {
+ }
+ }
+
+ ///
+ /// 联机模式
+ ///
+ internal sealed class HostPlayModeClearUnusedCacheFilesOperation : ClearUnusedCacheFilesOperation
+ {
+ private enum ESteps
+ {
+ None,
+ GetUnusedCacheFiles,
+ ClearUnusedCacheFiles,
+ Done,
+ }
+
+ private ESteps _steps = ESteps.None;
+ private List _unusedCacheFilePaths;
+ private int _unusedFileTotalCount = 0;
+ private HostPlayModeImpl _impl;
+
+ internal HostPlayModeClearUnusedCacheFilesOperation(HostPlayModeImpl impl)
+ {
+ _impl = impl;
+ }
+ internal override void Start()
+ {
+ _steps = ESteps.GetUnusedCacheFiles;
+ }
+ internal override void Update()
+ {
+ if (_steps == ESteps.None || _steps == ESteps.Done)
+ return;
+
+ if (_steps == ESteps.GetUnusedCacheFiles)
+ {
+ _unusedCacheFilePaths = _impl.ClearUnusedCacheFilePaths();
+ _unusedFileTotalCount = _unusedCacheFilePaths.Count;
+ YooLogger.Log($"Found unused cache file count : {_unusedFileTotalCount}");
+ _steps = ESteps.ClearUnusedCacheFiles;
+ }
+
+ if (_steps == ESteps.ClearUnusedCacheFiles)
+ {
+ for (int i = _unusedCacheFilePaths.Count - 1; i >= 0; i--)
+ {
+ string filePath = _unusedCacheFilePaths[i];
+ if (File.Exists(filePath))
+ {
+ YooLogger.Log($"Delete unused cache file : {filePath}");
+ File.Delete(filePath);
+ }
+ _unusedCacheFilePaths.RemoveAt(i);
+
+ if (OperationSystem.IsBusy)
+ break;
+ }
+
+ if (_unusedFileTotalCount == 0)
+ Progress = 1.0f;
+ else
+ Progress = 1.0f - (_unusedCacheFilePaths.Count / _unusedFileTotalCount);
+
+ if (_unusedCacheFilePaths.Count == 0)
+ {
+ _steps = ESteps.Done;
+ Status = EOperationStatus.Succeed;
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs.meta b/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs.meta
new file mode 100644
index 0000000..41bfef3
--- /dev/null
+++ b/Assets/YooAsset/Runtime/PatchSystem/Operations/ClearUnusedCacheFilesOperation.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 5d188c50fd00bf941b2eeebb374dc0d1
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs
index de3e60b..e668cfa 100644
--- a/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs
+++ b/Assets/YooAsset/Runtime/PatchSystem/PlayMode/HostPlayModeImpl.cs
@@ -88,16 +88,18 @@ namespace YooAsset
}
///
- /// 清空未被使用的缓存文件
+ /// 获取未被使用的缓存文件路径集合
///
- public void ClearUnusedCacheFiles()
+ public List ClearUnusedCacheFilePaths()
{
string cacheFolderPath = SandboxHelper.GetCacheFolderPath();
if (Directory.Exists(cacheFolderPath) == false)
- return;
+ return new List();
DirectoryInfo directoryInfo = new DirectoryInfo(cacheFolderPath);
- foreach (FileInfo fileInfo in directoryInfo.GetFiles())
+ FileInfo[] fileInfos = directoryInfo.GetFiles();
+ List result = new List(fileInfos.Length);
+ foreach (FileInfo fileInfo in fileInfos)
{
bool used = false;
foreach (var patchBundle in LocalPatchManifest.BundleList)
@@ -109,11 +111,9 @@ namespace YooAsset
}
}
if (used == false)
- {
- YooLogger.Log($"Delete unused cache file : {fileInfo.Name}");
- File.Delete(fileInfo.FullName);
- }
+ result.Add(fileInfo.FullName);
}
+ return result;
}
///
diff --git a/Assets/YooAsset/Runtime/YooAssets.cs b/Assets/YooAsset/Runtime/YooAssets.cs
index f28a4d4..dbb1ed1 100644
--- a/Assets/YooAsset/Runtime/YooAssets.cs
+++ b/Assets/YooAsset/Runtime/YooAssets.cs
@@ -203,7 +203,7 @@ namespace YooAsset
{
var hostPlayModeParameters = parameters as HostPlayModeParameters;
CacheSystem.Initialize(hostPlayModeParameters.VerifyLevel);
- DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize);
+ DownloadSystem.Initialize(hostPlayModeParameters.BreakpointResumeFileSize);
}
else
{
@@ -1040,10 +1040,31 @@ namespace YooAsset
///
/// 清空未被使用的缓存文件
///
- public static void ClearUnusedCacheFiles()
+ public static ClearUnusedCacheFilesOperation ClearUnusedCacheFiles()
{
- if (_playMode == EPlayMode.HostPlayMode)
- _hostPlayModeImpl.ClearUnusedCacheFiles();
+ DebugCheckInitialize();
+ if (_playMode == EPlayMode.EditorSimulateMode)
+ {
+ var operation = new EditorPlayModeClearUnusedCacheFilesOperation();
+ OperationSystem.StartOperation(operation);
+ return operation;
+ }
+ else if (_playMode == EPlayMode.OfflinePlayMode)
+ {
+ var operation = new OfflinePlayModeClearUnusedCacheFilesOperation();
+ OperationSystem.StartOperation(operation);
+ return operation;
+ }
+ else if (_playMode == EPlayMode.HostPlayMode)
+ {
+ var operation = new HostPlayModeClearUnusedCacheFilesOperation(_hostPlayModeImpl);
+ OperationSystem.StartOperation(operation);
+ return operation;
+ }
+ else
+ {
+ throw new NotImplementedException();
+ }
}
#endregion