编排跨帧游戏流程
驱动调度器
csharp
private readonly CoroutineScheduler _scheduler = new(timeSource, supportedStages:
[
CoroutineExecutionStage.Update,
CoroutineExecutionStage.FixedUpdate,
CoroutineExecutionStage.Deferred,
]);
/// <summary>Starts a flow with owned children and default FailFast behavior.</summary>
public CoroutineHandle Start() => _scheduler.Run(context => PlaySkill(context));
/// <summary>Called by the host once per matching phase with that phase's own time deltas.</summary>
public void Tick(CoroutineExecutionStage stage, double scaledDelta, double realDelta, bool paused)
{
_scheduler.Update(stage, scaledDelta, realDelta, paused);
}
/// <summary>Called by the host's shutdown path before the owning engine objects disappear.</summary>
public void Dispose() => _scheduler.Dispose();启动流程
csharp
/// <summary>Owns two concurrent steps and waits for them before its final result is available.</summary>
private static IEnumerator<IYieldInstruction> PlaySkill(CoroutineContext context)
{
context.RunChild(PlayEffect(), cancellationToken: global::System.Threading.CancellationToken.None);
context.RunChild(ApplyDamage(), cancellationToken: global::System.Threading.CancellationToken.None);
// Completing this iterator does not detach its children: the parent waits for both to finish.
yield break;
}
/// <summary>Runs in the initial phase, then moves to the host's deferred phase.</summary>
private static IEnumerator<IYieldInstruction> PlayEffect()
{
yield return new Delay(0.1);
yield return new SwitchTo(CoroutineExecutionStage.Deferred);
Console.WriteLine("Effect step in deferred phase");
}
/// <summary>Moves to the next fixed tick and remains there for subsequent ordinary waits.</summary>
private static IEnumerator<IYieldInstruction> ApplyDamage()
{
yield return new SwitchTo(CoroutineExecutionStage.FixedUpdate);
yield return new WaitOneFrame();
Console.WriteLine("Damage step in fixed phase");
}观察结束
csharp
/// <summary>Registers a completion task before cancellation, so bounded history cannot expire its result.</summary>
public async System.Threading.Tasks.Task CancelAndObserveAsync(CoroutineHandle handle)
{
var completion = _scheduler.WaitForResultAsync(handle);
_scheduler.Kill(handle);
var result = await completion.ConfigureAwait(false);
Console.WriteLine(result.Status);
Console.WriteLine(result.Exception);
}游戏关闭时由所属组件停止调度工作。接入原生阶段见Godot 协程。