refactor : 重构异步操作模块

This commit is contained in:
何冠峰
2026-01-13 12:08:02 +08:00
parent b796b1a44e
commit ce4d6911db
4 changed files with 64 additions and 10 deletions

View File

@@ -10,6 +10,7 @@ namespace YooAsset
{ {
private readonly List<AsyncOperationBase> _operations = new List<AsyncOperationBase>(100); private readonly List<AsyncOperationBase> _operations = new List<AsyncOperationBase>(100);
private readonly List<AsyncOperationBase> _newList = new List<AsyncOperationBase>(100); private readonly List<AsyncOperationBase> _newList = new List<AsyncOperationBase>(100);
private uint _priority = 0;
/// <summary> /// <summary>
/// 所属包裹名称 /// 所属包裹名称
@@ -19,7 +20,23 @@ namespace YooAsset
/// <summary> /// <summary>
/// 调度器优先级(值越大越优先) /// 调度器优先级(值越大越优先)
/// </summary> /// </summary>
public int Priority { private set; get; } public uint Priority
{
get { return _priority; }
set
{
if (_priority != value)
{
_priority = value;
IsDirty = true;
}
}
}
/// <summary>
/// 优先级是否已变更(需要重新排序)
/// </summary>
public bool IsDirty { set; get; } = false;
/// <summary> /// <summary>
/// 创建顺序(用于同优先级稳定排序) /// 创建顺序(用于同优先级稳定排序)
@@ -27,7 +44,7 @@ namespace YooAsset
public int CreateIndex { private set; get; } public int CreateIndex { private set; get; }
public OperationScheduler(string packageName, int priority, int createIndex) public OperationScheduler(string packageName, uint priority, int createIndex)
{ {
PackageName = packageName; PackageName = packageName;
Priority = priority; Priority = priority;

View File

@@ -21,7 +21,6 @@ namespace YooAsset
private static readonly Dictionary<string, OperationScheduler> _schedulerDic = new Dictionary<string, OperationScheduler>(100); private static readonly Dictionary<string, OperationScheduler> _schedulerDic = new Dictionary<string, OperationScheduler>(100);
private static readonly List<OperationScheduler> _schedulerList = new List<OperationScheduler>(100); private static readonly List<OperationScheduler> _schedulerList = new List<OperationScheduler>(100);
private static bool _isInitialize = false; private static bool _isInitialize = false;
private static bool _schedulerListDirty = false;
private static int _createIndex = 0; private static int _createIndex = 0;
// 计时器相关 // 计时器相关
@@ -75,10 +74,18 @@ namespace YooAsset
if (_isInitialize == false) if (_isInitialize == false)
return; return;
// 重新排序调度器 // 检测是否需要执行排序
if (_schedulerListDirty) bool isDirty = false;
foreach (var scheduler in _schedulerList)
{
if (scheduler.IsDirty)
{
scheduler.IsDirty = false;
isDirty = true;
}
}
if (isDirty)
{ {
_schedulerListDirty = false;
_schedulerList.Sort(); _schedulerList.Sort();
} }
@@ -110,7 +117,6 @@ namespace YooAsset
} }
_schedulerDic.Clear(); _schedulerDic.Clear();
_schedulerList.Clear(); _schedulerList.Clear();
_schedulerListDirty = false;
_createIndex = 0; _createIndex = 0;
_watch = null; _watch = null;
@@ -121,7 +127,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 创建包裹调度器 /// 创建包裹调度器
/// </summary> /// </summary>
public static void CreatePackageScheduler(string packageName, int priority) public static OperationScheduler CreatePackageScheduler(string packageName, uint priority)
{ {
DebugCheckInitialize(packageName); DebugCheckInitialize(packageName);
@@ -133,7 +139,7 @@ namespace YooAsset
var scheduler = new OperationScheduler(packageName, priority, _createIndex++); var scheduler = new OperationScheduler(packageName, priority, _createIndex++);
_schedulerDic.Add(packageName, scheduler); _schedulerDic.Add(packageName, scheduler);
_schedulerList.Add(scheduler); _schedulerList.Add(scheduler);
_schedulerListDirty = true; return scheduler;
} }
/// <summary> /// <summary>
@@ -179,6 +185,28 @@ namespace YooAsset
scheduler.StartOperation(operation); scheduler.StartOperation(operation);
} }
/// <summary>
/// 设置调度器优先级
/// </summary>
public static void SetSchedulerPriority(string packageName, uint priority)
{
DebugCheckInitialize(packageName);
var scheduler = GetScheduler(packageName);
scheduler.Priority = priority;
}
/// <summary>
/// 获取调度器优先级
/// </summary>
public static uint GetSchedulerPriority(string packageName)
{
DebugCheckInitialize(packageName);
var scheduler = GetScheduler(packageName);
return scheduler.Priority;
}
/// <summary> /// <summary>
/// 获取调度器(严格模式) /// 获取调度器(严格模式)
/// </summary> /// </summary>

View File

@@ -44,6 +44,15 @@ namespace YooAsset
} }
} }
/// <summary>
/// 包裹优先级(值越大越优先更新)
/// </summary>
public uint PackagePriority
{
get { return OperationSystem.GetSchedulerPriority(PackageName); }
set { OperationSystem.SetSchedulerPriority(PackageName, value); }
}
internal ResourcePackage(string packageName) internal ResourcePackage(string packageName)
{ {

View File

@@ -112,7 +112,7 @@ namespace YooAsset
/// </summary> /// </summary>
/// <param name="packageName">包裹名称</param> /// <param name="packageName">包裹名称</param>
/// <param name="packagePriority">包裹优先级(值越大越优先更新)</param> /// <param name="packagePriority">包裹优先级(值越大越优先更新)</param>
public static ResourcePackage CreatePackage(string packageName, int packagePriority) public static ResourcePackage CreatePackage(string packageName, uint packagePriority)
{ {
CheckException(packageName); CheckException(packageName);
if (ContainsPackage(packageName)) if (ContainsPackage(packageName))