From 354ca5197ff7f9d2826ef80c1118c8145ef72d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Tue, 13 Jan 2026 14:55:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=E9=87=8D=E6=9E=84=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E6=93=8D=E4=BD=9C=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UnityWebRequestDownloaderBase.cs | 7 +---- .../VirtualFileDownloader.cs | 13 ++------ .../internal/SearchCacheFilesOperation.cs | 6 ++-- .../internal/VerifyCacheFilesOperation.cs | 6 ++-- .../OperationSystem/AsyncOperationBase.cs | 24 +++++++++----- .../OperationSystem/OperationScheduler.cs | 3 +- .../OperationSystem/OperationSystem.cs | 5 +-- .../Runtime/OperationSystem/README.md | 31 +++++++++++++++++++ Assets/YooAsset/Runtime/Utility/YooUtility.cs | 21 +++++++++++++ 9 files changed, 82 insertions(+), 34 deletions(-) diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestDownloaderBase.cs b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestDownloaderBase.cs index f6e6f680..5f79567d 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestDownloaderBase.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestDownloaderBase.cs @@ -255,12 +255,7 @@ namespace YooAsset if (_watchdogAborted) return; -#if UNITY_2020_3_OR_NEWER - double realtimeSinceStartup = UnityEngine.Time.realtimeSinceStartupAsDouble; -#else - double realtimeSinceStartup = UnityEngine.Time.realtimeSinceStartup; -#endif - + double realtimeSinceStartup = TimeUtility.RealtimeSinceStartup; if (DownloadedBytes != _lastDownloadBytes) { _lastDownloadBytes = DownloadedBytes; diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/VirtualFileDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/VirtualFileDownloader.cs index 96010902..aa6a5c84 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/VirtualFileDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/VirtualFileDownloader.cs @@ -87,7 +87,7 @@ namespace YooAsset if (Status == EDownloadRequestStatus.None) { Status = EDownloadRequestStatus.Running; - _lastUpdateTime = GetUnityEngineRealtime(); + _lastUpdateTime = TimeUtility.RealtimeSinceStartup; } } @@ -99,7 +99,7 @@ namespace YooAsset if (Status != EDownloadRequestStatus.Running) return; - double currentTime = GetUnityEngineRealtime(); + double currentTime = TimeUtility.RealtimeSinceStartup; double deltaTime = currentTime - _lastUpdateTime; _lastUpdateTime = currentTime; @@ -137,14 +137,5 @@ namespace YooAsset public void Dispose() { } - - private double GetUnityEngineRealtime() - { -#if UNITY_2020_3_OR_NEWER - return UnityEngine.Time.realtimeSinceStartupAsDouble; -#else - return UnityEngine.Time.realtimeSinceStartup; -#endif - } } } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs index a297e886..4239e8a3 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/SearchCacheFilesOperation.cs @@ -17,7 +17,7 @@ namespace YooAsset private readonly DefaultCacheFileSystem _fileSystem; private IEnumerator _filesEnumerator = null; - private float _verifyStartTime; + private double _verifyStartTime; private ESteps _steps = ESteps.None; /// @@ -33,7 +33,7 @@ namespace YooAsset internal override void InternalStart() { _steps = ESteps.Prepare; - _verifyStartTime = UnityEngine.Time.realtimeSinceStartup; + _verifyStartTime = TimeUtility.RealtimeSinceStartup; } internal override void InternalUpdate() { @@ -58,7 +58,7 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Succeed; - float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime; + double costTime = TimeUtility.RealtimeSinceStartup - _verifyStartTime; YooLogger.Log($"Search cache files elapsed time {costTime:f1} seconds"); } } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs index c6581091..e38961a4 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyCacheFilesOperation.cs @@ -25,7 +25,7 @@ namespace YooAsset private List _verifyingList; private int _verifyMaxNum; private int _verifyTotalCount; - private float _verifyStartTime; + private double _verifyStartTime; private int _succeedCount; private int _failedCount; private ESteps _steps = ESteps.None; @@ -40,7 +40,7 @@ namespace YooAsset internal override void InternalStart() { _steps = ESteps.InitVerify; - _verifyStartTime = UnityEngine.Time.realtimeSinceStartup; + _verifyStartTime = TimeUtility.RealtimeSinceStartup; } internal override void InternalUpdate() { @@ -84,7 +84,7 @@ namespace YooAsset { _steps = ESteps.Done; Status = EOperationStatus.Succeed; - float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime; + double costTime = TimeUtility.RealtimeSinceStartup - _verifyStartTime; YooLogger.Log($"Verify cache files elapsed time {costTime:f1} seconds"); } diff --git a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs index cb621a75..9bef8795 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs @@ -213,7 +213,16 @@ namespace YooAsset DebugBeginRecording(); // 开始任务 - InternalStart(); + try + { + InternalStart(); + } + catch (Exception ex) + { + Status = EOperationStatus.Failed; + Error = ex.ToString(); + YooLogger.Error($"Exception in {this.GetType().Name}.InternalStart : {ex}"); + } } } @@ -288,6 +297,7 @@ namespace YooAsset try { + //TODO 单个回调异常会阻断后续回调 _callback?.Invoke(this); } catch (Exception ex) @@ -392,7 +402,7 @@ namespace YooAsset /// /// 开始的时间 /// - public string BeginTime = string.Empty; + public string BeginTime { protected set; get; } /// /// 处理耗时(单位:毫秒) @@ -407,7 +417,7 @@ namespace YooAsset { if (_watch == null) { - BeginTime = SpawnTimeToString(UnityEngine.Time.realtimeSinceStartup); + BeginTime = SpawnTimeToString(TimeUtility.RealtimeSinceStartup); _watch = Stopwatch.StartNew(); } } @@ -431,11 +441,11 @@ namespace YooAsset } } - private string SpawnTimeToString(float spawnTime) + private string SpawnTimeToString(double spawnTime) { - float h = UnityEngine.Mathf.FloorToInt(spawnTime / 3600f); - float m = UnityEngine.Mathf.FloorToInt(spawnTime / 60f - h * 60f); - float s = UnityEngine.Mathf.FloorToInt(spawnTime - m * 60f - h * 3600f); + double h = System.Math.Floor(spawnTime / 3600); + double m = System.Math.Floor(spawnTime / 60 - h * 60); + double s = System.Math.Floor(spawnTime - m * 60 - h * 3600); return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00"); } private bool WouldCreateCycle(AsyncOperationBase child) diff --git a/Assets/YooAsset/Runtime/OperationSystem/OperationScheduler.cs b/Assets/YooAsset/Runtime/OperationSystem/OperationScheduler.cs index cabfe18e..a146543f 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/OperationScheduler.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/OperationScheduler.cs @@ -44,10 +44,9 @@ namespace YooAsset public int CreateIndex { private set; get; } - public OperationScheduler(string packageName, uint priority, int createIndex) + public OperationScheduler(string packageName, int createIndex) { PackageName = packageName; - Priority = priority; CreateIndex = createIndex; } diff --git a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs index ac2709f3..ea2439da 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/OperationSystem.cs @@ -62,7 +62,7 @@ namespace YooAsset _watch = Stopwatch.StartNew(); // 创建全局调度器 - CreatePackageScheduler(GLOBAL_SCHEDULER_NAME, int.MaxValue); + CreatePackageScheduler(GLOBAL_SCHEDULER_NAME, uint.MaxValue); } } @@ -136,9 +136,10 @@ namespace YooAsset throw new YooInternalException($"Package scheduler already exists: {packageName}"); } - var scheduler = new OperationScheduler(packageName, priority, _createIndex++); + var scheduler = new OperationScheduler(packageName, _createIndex++); _schedulerDic.Add(packageName, scheduler); _schedulerList.Add(scheduler); + scheduler.Priority = priority; return scheduler; } diff --git a/Assets/YooAsset/Runtime/OperationSystem/README.md b/Assets/YooAsset/Runtime/OperationSystem/README.md index 5cd39d31..60b1f76a 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/README.md +++ b/Assets/YooAsset/Runtime/OperationSystem/README.md @@ -217,6 +217,16 @@ public static void ClearPackageOperation(string packageName); /// 启动异步操作 /// public static void StartOperation(string packageName, AsyncOperationBase operation); + +/// +/// 设置调度器优先级 +/// +public static void SetSchedulerPriority(string packageName, uint priority); + +/// +/// 获取调度器优先级 +/// +public static uint GetSchedulerPriority(string packageName); ``` #### 包裹调度说明 @@ -315,11 +325,32 @@ void LoadAssetSync() 操作按 `Priority` 属性降序排列,优先级高的操作先执行。 +#### 操作优先级 + ```csharp var operation = package.LoadAssetAsync(location); operation.Priority = 100; // 设置高优先级 ``` +#### 包裹优先级 + +通过 `ResourcePackage.PackagePriority` 可以设置包裹的调度器优先级,值越大越优先更新。 + +```csharp +// 创建包裹时指定优先级 +var package = YooAssets.CreatePackage("MyPackage", 100); + +// 运行时动态调整优先级 +package.PackagePriority = 200; + +// 获取当前优先级 +uint priority = package.PackagePriority; +``` + +**使用场景:** +- 多包裹场景下,可根据游戏状态动态调整包裹优先级 +- 例如:进入战斗时提高战斗资源包的优先级,退出战斗时恢复默认优先级 + **排序规则:** - 新操作添加时:若新增队列存在非零优先级,则触发排序 - 运行中修改 `Priority`:调度器会在每帧 `Update()` 的排序阶段检测 `IsDirty` 并触发重排;若在某个操作的 `InternalUpdate()` 内修改(本帧排序已完成),则新的优先级会延后一帧生效(可能与预期不符) diff --git a/Assets/YooAsset/Runtime/Utility/YooUtility.cs b/Assets/YooAsset/Runtime/Utility/YooUtility.cs index 85e4af84..64c09062 100644 --- a/Assets/YooAsset/Runtime/Utility/YooUtility.cs +++ b/Assets/YooAsset/Runtime/Utility/YooUtility.cs @@ -5,6 +5,27 @@ using System.Text; namespace YooAsset { + /// + /// 时间工具类 + /// + internal static class TimeUtility + { + /// + /// The real time in seconds since the game started + /// + public static double RealtimeSinceStartup + { + get + { +#if UNITY_2020_3_OR_NEWER + return UnityEngine.Time.realtimeSinceStartupAsDouble; +#else + return UnityEngine.Time.realtimeSinceStartup; +#endif + } + } + } + /// /// 路径工具类 ///