mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-30 05:28:46 +00:00
refactor : 重构异步操作模块
This commit is contained in:
@@ -255,12 +255,7 @@ namespace YooAsset
|
|||||||
if (_watchdogAborted)
|
if (_watchdogAborted)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
#if UNITY_2020_3_OR_NEWER
|
double realtimeSinceStartup = TimeUtility.RealtimeSinceStartup;
|
||||||
double realtimeSinceStartup = UnityEngine.Time.realtimeSinceStartupAsDouble;
|
|
||||||
#else
|
|
||||||
double realtimeSinceStartup = UnityEngine.Time.realtimeSinceStartup;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (DownloadedBytes != _lastDownloadBytes)
|
if (DownloadedBytes != _lastDownloadBytes)
|
||||||
{
|
{
|
||||||
_lastDownloadBytes = DownloadedBytes;
|
_lastDownloadBytes = DownloadedBytes;
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ namespace YooAsset
|
|||||||
if (Status == EDownloadRequestStatus.None)
|
if (Status == EDownloadRequestStatus.None)
|
||||||
{
|
{
|
||||||
Status = EDownloadRequestStatus.Running;
|
Status = EDownloadRequestStatus.Running;
|
||||||
_lastUpdateTime = GetUnityEngineRealtime();
|
_lastUpdateTime = TimeUtility.RealtimeSinceStartup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ namespace YooAsset
|
|||||||
if (Status != EDownloadRequestStatus.Running)
|
if (Status != EDownloadRequestStatus.Running)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
double currentTime = GetUnityEngineRealtime();
|
double currentTime = TimeUtility.RealtimeSinceStartup;
|
||||||
double deltaTime = currentTime - _lastUpdateTime;
|
double deltaTime = currentTime - _lastUpdateTime;
|
||||||
_lastUpdateTime = currentTime;
|
_lastUpdateTime = currentTime;
|
||||||
|
|
||||||
@@ -137,14 +137,5 @@ namespace YooAsset
|
|||||||
public void Dispose()
|
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 readonly DefaultCacheFileSystem _fileSystem;
|
||||||
private IEnumerator<string> _filesEnumerator = null;
|
private IEnumerator<string> _filesEnumerator = null;
|
||||||
private float _verifyStartTime;
|
private double _verifyStartTime;
|
||||||
private ESteps _steps = ESteps.None;
|
private ESteps _steps = ESteps.None;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -33,7 +33,7 @@ namespace YooAsset
|
|||||||
internal override void InternalStart()
|
internal override void InternalStart()
|
||||||
{
|
{
|
||||||
_steps = ESteps.Prepare;
|
_steps = ESteps.Prepare;
|
||||||
_verifyStartTime = UnityEngine.Time.realtimeSinceStartup;
|
_verifyStartTime = TimeUtility.RealtimeSinceStartup;
|
||||||
}
|
}
|
||||||
internal override void InternalUpdate()
|
internal override void InternalUpdate()
|
||||||
{
|
{
|
||||||
@@ -58,7 +58,7 @@ namespace YooAsset
|
|||||||
|
|
||||||
_steps = ESteps.Done;
|
_steps = ESteps.Done;
|
||||||
Status = EOperationStatus.Succeed;
|
Status = EOperationStatus.Succeed;
|
||||||
float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime;
|
double costTime = TimeUtility.RealtimeSinceStartup - _verifyStartTime;
|
||||||
YooLogger.Log($"Search cache files elapsed time {costTime:f1} seconds");
|
YooLogger.Log($"Search cache files elapsed time {costTime:f1} seconds");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ namespace YooAsset
|
|||||||
private List<VerifyFileElement> _verifyingList;
|
private List<VerifyFileElement> _verifyingList;
|
||||||
private int _verifyMaxNum;
|
private int _verifyMaxNum;
|
||||||
private int _verifyTotalCount;
|
private int _verifyTotalCount;
|
||||||
private float _verifyStartTime;
|
private double _verifyStartTime;
|
||||||
private int _succeedCount;
|
private int _succeedCount;
|
||||||
private int _failedCount;
|
private int _failedCount;
|
||||||
private ESteps _steps = ESteps.None;
|
private ESteps _steps = ESteps.None;
|
||||||
@@ -40,7 +40,7 @@ namespace YooAsset
|
|||||||
internal override void InternalStart()
|
internal override void InternalStart()
|
||||||
{
|
{
|
||||||
_steps = ESteps.InitVerify;
|
_steps = ESteps.InitVerify;
|
||||||
_verifyStartTime = UnityEngine.Time.realtimeSinceStartup;
|
_verifyStartTime = TimeUtility.RealtimeSinceStartup;
|
||||||
}
|
}
|
||||||
internal override void InternalUpdate()
|
internal override void InternalUpdate()
|
||||||
{
|
{
|
||||||
@@ -84,7 +84,7 @@ namespace YooAsset
|
|||||||
{
|
{
|
||||||
_steps = ESteps.Done;
|
_steps = ESteps.Done;
|
||||||
Status = EOperationStatus.Succeed;
|
Status = EOperationStatus.Succeed;
|
||||||
float costTime = UnityEngine.Time.realtimeSinceStartup - _verifyStartTime;
|
double costTime = TimeUtility.RealtimeSinceStartup - _verifyStartTime;
|
||||||
YooLogger.Log($"Verify cache files elapsed time {costTime:f1} seconds");
|
YooLogger.Log($"Verify cache files elapsed time {costTime:f1} seconds");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -213,8 +213,17 @@ namespace YooAsset
|
|||||||
DebugBeginRecording();
|
DebugBeginRecording();
|
||||||
|
|
||||||
// 开始任务
|
// 开始任务
|
||||||
|
try
|
||||||
|
{
|
||||||
InternalStart();
|
InternalStart();
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Status = EOperationStatus.Failed;
|
||||||
|
Error = ex.ToString();
|
||||||
|
YooLogger.Error($"Exception in {this.GetType().Name}.InternalStart : {ex}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -288,6 +297,7 @@ namespace YooAsset
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
//TODO 单个回调异常会阻断后续回调
|
||||||
_callback?.Invoke(this);
|
_callback?.Invoke(this);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@@ -392,7 +402,7 @@ namespace YooAsset
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 开始的时间
|
/// 开始的时间
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string BeginTime = string.Empty;
|
public string BeginTime { protected set; get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 处理耗时(单位:毫秒)
|
/// 处理耗时(单位:毫秒)
|
||||||
@@ -407,7 +417,7 @@ namespace YooAsset
|
|||||||
{
|
{
|
||||||
if (_watch == null)
|
if (_watch == null)
|
||||||
{
|
{
|
||||||
BeginTime = SpawnTimeToString(UnityEngine.Time.realtimeSinceStartup);
|
BeginTime = SpawnTimeToString(TimeUtility.RealtimeSinceStartup);
|
||||||
_watch = Stopwatch.StartNew();
|
_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);
|
double h = System.Math.Floor(spawnTime / 3600);
|
||||||
float m = UnityEngine.Mathf.FloorToInt(spawnTime / 60f - h * 60f);
|
double m = System.Math.Floor(spawnTime / 60 - h * 60);
|
||||||
float s = UnityEngine.Mathf.FloorToInt(spawnTime - m * 60f - h * 3600f);
|
double s = System.Math.Floor(spawnTime - m * 60 - h * 3600);
|
||||||
return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00");
|
return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00");
|
||||||
}
|
}
|
||||||
private bool WouldCreateCycle(AsyncOperationBase child)
|
private bool WouldCreateCycle(AsyncOperationBase child)
|
||||||
|
|||||||
@@ -44,10 +44,9 @@ namespace YooAsset
|
|||||||
public int CreateIndex { private set; get; }
|
public int CreateIndex { private set; get; }
|
||||||
|
|
||||||
|
|
||||||
public OperationScheduler(string packageName, uint priority, int createIndex)
|
public OperationScheduler(string packageName, int createIndex)
|
||||||
{
|
{
|
||||||
PackageName = packageName;
|
PackageName = packageName;
|
||||||
Priority = priority;
|
|
||||||
CreateIndex = createIndex;
|
CreateIndex = createIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ namespace YooAsset
|
|||||||
_watch = Stopwatch.StartNew();
|
_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}");
|
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);
|
_schedulerDic.Add(packageName, scheduler);
|
||||||
_schedulerList.Add(scheduler);
|
_schedulerList.Add(scheduler);
|
||||||
|
scheduler.Priority = priority;
|
||||||
return scheduler;
|
return scheduler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -217,6 +217,16 @@ public static void ClearPackageOperation(string packageName);
|
|||||||
/// 启动异步操作
|
/// 启动异步操作
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void StartOperation(string packageName, AsyncOperationBase operation);
|
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` 属性降序排列,优先级高的操作先执行。
|
操作按 `Priority` 属性降序排列,优先级高的操作先执行。
|
||||||
|
|
||||||
|
#### 操作优先级
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var operation = package.LoadAssetAsync<GameObject>(location);
|
var operation = package.LoadAssetAsync<GameObject>(location);
|
||||||
operation.Priority = 100; // 设置高优先级
|
operation.Priority = 100; // 设置高优先级
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### 包裹优先级
|
||||||
|
|
||||||
|
通过 `ResourcePackage.PackagePriority` 可以设置包裹的调度器优先级,值越大越优先更新。
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// 创建包裹时指定优先级
|
||||||
|
var package = YooAssets.CreatePackage("MyPackage", 100);
|
||||||
|
|
||||||
|
// 运行时动态调整优先级
|
||||||
|
package.PackagePriority = 200;
|
||||||
|
|
||||||
|
// 获取当前优先级
|
||||||
|
uint priority = package.PackagePriority;
|
||||||
|
```
|
||||||
|
|
||||||
|
**使用场景:**
|
||||||
|
- 多包裹场景下,可根据游戏状态动态调整包裹优先级
|
||||||
|
- 例如:进入战斗时提高战斗资源包的优先级,退出战斗时恢复默认优先级
|
||||||
|
|
||||||
**排序规则:**
|
**排序规则:**
|
||||||
- 新操作添加时:若新增队列存在非零优先级,则触发排序
|
- 新操作添加时:若新增队列存在非零优先级,则触发排序
|
||||||
- 运行中修改 `Priority`:调度器会在每帧 `Update()` 的排序阶段检测 `IsDirty` 并触发重排;若在某个操作的 `InternalUpdate()` 内修改(本帧排序已完成),则新的优先级会延后一帧生效(可能与预期不符)
|
- 运行中修改 `Priority`:调度器会在每帧 `Update()` 的排序阶段检测 `IsDirty` 并触发重排;若在某个操作的 `InternalUpdate()` 内修改(本帧排序已完成),则新的优先级会延后一帧生效(可能与预期不符)
|
||||||
|
|||||||
@@ -5,6 +5,27 @@ using System.Text;
|
|||||||
|
|
||||||
namespace YooAsset
|
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>
|
||||||
/// 路径工具类
|
/// 路径工具类
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user