mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-23 03:04:29 +08:00
- 移除多余using语句和空行,统一代码缩进格式 - 优化注释文档中的缩进和对齐格式 - 简化条件语句和方法实现,提升代码可读性 - 重构协程系统相关类的字段和方法定义格式 - 更新架构服务中容器访问方式的实现 - 调整异步操作类的属性和方法组织结构 - [skip ci]
64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using GFramework.Core.Abstractions.coroutine;
|
|
using GFramework.Core.coroutine;
|
|
using Godot;
|
|
|
|
namespace GFramework.Godot.coroutine;
|
|
|
|
public static class CoroutineExtensions
|
|
{
|
|
/// <summary>
|
|
/// 启动协程的扩展方法
|
|
/// </summary>
|
|
public static CoroutineHandle RunCoroutine(
|
|
this IEnumerator<IYieldInstruction> coroutine,
|
|
Segment segment = Segment.Process,
|
|
string? tag = null)
|
|
{
|
|
return Timing.RunCoroutine(coroutine, segment, tag);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 让协程在指定节点被销毁时自动取消
|
|
/// </summary>
|
|
public static IEnumerator<IYieldInstruction> CancelWith(
|
|
this IEnumerator<IYieldInstruction> coroutine,
|
|
Node node)
|
|
{
|
|
while (Timing.IsNodeAlive(node) && coroutine.MoveNext())
|
|
yield return coroutine.Current;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 让协程在任一节点被销毁时自动取消
|
|
/// </summary>
|
|
public static IEnumerator<IYieldInstruction> CancelWith(
|
|
this IEnumerator<IYieldInstruction> coroutine,
|
|
Node node1,
|
|
Node node2)
|
|
{
|
|
while (Timing.IsNodeAlive(node1) &&
|
|
Timing.IsNodeAlive(node2) &&
|
|
coroutine.MoveNext())
|
|
yield return coroutine.Current;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 让协程在多个节点都被销毁时自动取消
|
|
/// </summary>
|
|
public static IEnumerator<IYieldInstruction> CancelWith(
|
|
this IEnumerator<IYieldInstruction> coroutine,
|
|
params Node[] nodes)
|
|
{
|
|
while (AllNodesAlive(nodes) && coroutine.MoveNext())
|
|
yield return coroutine.Current;
|
|
}
|
|
|
|
private static bool AllNodesAlive(Node[] nodes)
|
|
{
|
|
foreach (var node in nodes)
|
|
if (!Timing.IsNodeAlive(node))
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
} |