mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
fix(coroutine): 修复Timing单例初始化逻辑
- 添加了GFramework.Godot.extensions命名空间引用 - 修正了Timing实例获取逻辑,避免重复创建已存在的实例 - 将AddChild操作包装在WaitUntilReady回调中确保正确添加到场景树 - 添加了WaitUntilReady的Action回调重载方法 - 实现了节点准备就绪后执行回调的功能 - 保持了原有异步等待ready信号的核心功能 - [release ci]
This commit is contained in:
parent
af583c101c
commit
4943fc5c70
@ -2,6 +2,7 @@
|
||||
using GFramework.Core.Abstractions.coroutine;
|
||||
using GFramework.Core.coroutine;
|
||||
using GFramework.Core.coroutine.instructions;
|
||||
using GFramework.Godot.extensions;
|
||||
using Godot;
|
||||
|
||||
namespace GFramework.Godot.coroutine;
|
||||
@ -62,16 +63,17 @@ public partial class Timing : Node
|
||||
|
||||
var tree = (SceneTree)Engine.GetMainLoop();
|
||||
_instance = tree.Root.GetNodeOrNull<Timing>(nameof(Timing));
|
||||
|
||||
if (_instance == null)
|
||||
if (_instance != null)
|
||||
{
|
||||
_instance = new Timing
|
||||
{
|
||||
Name = nameof(Timing)
|
||||
};
|
||||
tree.Root.AddChild(_instance);
|
||||
return _instance;
|
||||
}
|
||||
|
||||
_instance = new Timing
|
||||
{
|
||||
Name = nameof(Timing)
|
||||
};
|
||||
tree.Root.WaitUntilReady(() => { tree.Root.AddChild(_instance); });
|
||||
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,11 +49,40 @@ public static class NodeExtensions
|
||||
/// 如果节点尚未进入场景树,则等待 ready 信号。
|
||||
/// 如果已经在场景树中,则立刻返回。
|
||||
/// </summary>
|
||||
/// <param name="node">要等待其准备就绪的节点</param>
|
||||
/// <returns>表示异步操作的任务</returns>
|
||||
public static async Task WaitUntilReady(this Node node)
|
||||
{
|
||||
if (!node.IsInsideTree()) await node.ToSignal(node, Node.SignalName.Ready);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 如果节点尚未进入场景树,则等待 ready 信号后执行回调函数。
|
||||
/// 如果已经在场景树中,则立即执行回调函数。
|
||||
/// </summary>
|
||||
/// <param name="node">要等待其准备就绪的节点</param>
|
||||
/// <param name="callback">节点准备就绪后要执行的回调函数</param>
|
||||
public static void WaitUntilReady(this Node node, Action callback)
|
||||
{
|
||||
// 检查节点是否已经在场景树中
|
||||
if (node.IsInsideTree())
|
||||
{
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
|
||||
_ = WaitAsync();
|
||||
return;
|
||||
|
||||
// 异步等待节点准备就绪并执行回调
|
||||
async Task WaitAsync()
|
||||
{
|
||||
await node.ToSignal(node, Node.SignalName.Ready);
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 检查节点是否有效:
|
||||
/// 1. 非 null
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user