mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-26 10:40:14 +00:00
refactor : 重构异步操作模块
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace YooAsset
|
||||
|
||||
private readonly DefaultCacheFileSystem _fileSystem;
|
||||
private IEnumerator<string> _filesEnumerator = null;
|
||||
private float _verifyStartTime;
|
||||
private double _verifyStartTime;
|
||||
private ESteps _steps = ESteps.None;
|
||||
|
||||
/// <summary>
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace YooAsset
|
||||
private List<VerifyFileElement> _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");
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
/// <summary>
|
||||
/// 开始的时间
|
||||
/// </summary>
|
||||
public string BeginTime = string.Empty;
|
||||
public string BeginTime { protected set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// 处理耗时(单位:毫秒)
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -217,6 +217,16 @@ public static void ClearPackageOperation(string packageName);
|
||||
/// 启动异步操作
|
||||
/// </summary>
|
||||
public static void StartOperation(string packageName, AsyncOperationBase operation);
|
||||
|
||||
/// <summary>
|
||||
/// 设置调度器优先级
|
||||
/// </summary>
|
||||
public static void SetSchedulerPriority(string packageName, uint priority);
|
||||
|
||||
/// <summary>
|
||||
/// 获取调度器优先级
|
||||
/// </summary>
|
||||
public static uint GetSchedulerPriority(string packageName);
|
||||
```
|
||||
|
||||
#### 包裹调度说明
|
||||
@@ -315,11 +325,32 @@ void LoadAssetSync()
|
||||
|
||||
操作按 `Priority` 属性降序排列,优先级高的操作先执行。
|
||||
|
||||
#### 操作优先级
|
||||
|
||||
```csharp
|
||||
var operation = package.LoadAssetAsync<GameObject>(location);
|
||||
operation.Priority = 100; // 设置高优先级
|
||||
```
|
||||
|
||||
#### 包裹优先级
|
||||
|
||||
通过 `ResourcePackage.PackagePriority` 可以设置包裹的调度器优先级,值越大越优先更新。
|
||||
|
||||
```csharp
|
||||
// 创建包裹时指定优先级
|
||||
var package = YooAssets.CreatePackage("MyPackage", 100);
|
||||
|
||||
// 运行时动态调整优先级
|
||||
package.PackagePriority = 200;
|
||||
|
||||
// 获取当前优先级
|
||||
uint priority = package.PackagePriority;
|
||||
```
|
||||
|
||||
**使用场景:**
|
||||
- 多包裹场景下,可根据游戏状态动态调整包裹优先级
|
||||
- 例如:进入战斗时提高战斗资源包的优先级,退出战斗时恢复默认优先级
|
||||
|
||||
**排序规则:**
|
||||
- 新操作添加时:若新增队列存在非零优先级,则触发排序
|
||||
- 运行中修改 `Priority`:调度器会在每帧 `Update()` 的排序阶段检测 `IsDirty` 并触发重排;若在某个操作的 `InternalUpdate()` 内修改(本帧排序已完成),则新的优先级会延后一帧生效(可能与预期不符)
|
||||
|
||||
@@ -5,6 +5,27 @@ using System.Text;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 时间工具类
|
||||
/// </summary>
|
||||
internal static class TimeUtility
|
||||
{
|
||||
/// <summary>
|
||||
/// The real time in seconds since the game started
|
||||
/// </summary>
|
||||
public static double RealtimeSinceStartup
|
||||
{
|
||||
get
|
||||
{
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
return UnityEngine.Time.realtimeSinceStartupAsDouble;
|
||||
#else
|
||||
return UnityEngine.Time.realtimeSinceStartup;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 路径工具类
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user