接入 Godot
构建与运行
使用 Godot 4.7.2 .NET 和仓库指定的 SDK,从仓库根目录运行:
bash
dotnet build Samples/GodotIntegration/GodotIntegration.csproj -c Debug
godot --headless --editor --path Samples/GodotIntegration --import
godot --path Samples/GodotIntegration res://GettingStarted/QuickStart.tscn窗口显示按键加载的资源标题。打开确认框并操作,状态栏显示 Confirmed 或 Cancelled。
装配服务与视图
在场景根准备导出的资源与视图,在组合根登记资源组件、原生挂载根和类型化路由。资源完成初始化后,RegisterGodotScene<QuickStartPage, int> 的回调取得资源,并在页面挂载前完成配置。
csharp
// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using System.Threading;
using System.Threading.Tasks;
using GFramework.Core.Abstractions.Architectures;
using GFramework.Core.Architectures;
using GFramework.Game.Abstractions.Runtime;
using GFramework.Game.Abstractions.UI;
using GFramework.Game.Runtime;
using GFramework.Game.UI;
using GFramework.Godot.UI;
using Microsoft.Extensions.DependencyInjection;
namespace GodotIntegration.GettingStarted;
/// <summary>Composes ordinary services, explicitly ordered startup and the borrowed native host.</summary>
/// <remarks>All container access stays here. Native attachment follows resource validation; detachment follows UI stop.</remarks>
/// <param name="root">The borrowed scene and resource owner.</param>
/// <param name="dispatcher">The owning Godot thread.</param>
internal sealed class QuickStartArchitecture(QuickStartRoot root, IGameDispatcher dispatcher) : Architecture, IAsyncArchitectureHost
{
/// <summary>Describes dependencies and startup participants before any service is activated.</summary>
protected override void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(root);
services.AddSingleton(dispatcher);
services.AddComponent<QuickStartResources>(order: -100);
services.AddSingleton(provider => CreateRoutes(provider.GetRequiredService<QuickStartResources>()));
services.AddModule(new GameRuntimeModule(dispatcher, runtime =>
{
runtime.AddSingleton<IUiService>(provider => new UiService(provider.GetRequiredService<UiRegistry>(),
root.UiRoot, dispatcher));
}));
}
private UiRegistry CreateRoutes(QuickStartResources resources)
{
var routes = new UiRegistry();
routes.RegisterGodotScene<QuickStartPage, int>(QuickStartRoot.PageRoute, root.PageScene, (page, _, _) =>
page.Configure(resources.Get("welcome"), root.RequestConfirmation));
routes.RegisterGodotDialog(QuickStartRoot.ConfirmRoute, (question, context) =>
{
var node = root.ConfirmScene.Instantiate();
try
{
var confirm = (QuickStartConfirm)node;
confirm.Configure(question, context, root.RunResponse);
return confirm;
}
catch
{
node.Free();
throw;
}
});
return routes;
}
/// <inheritdoc />
ValueTask IAsyncArchitectureHost.StartHostAsync(CancellationToken cancellationToken) =>
new(dispatcher.InvokeAsync(() => root.AttachUiAsync(Services.GetRequiredService<IUiService>()), cancellationToken));
/// <inheritdoc />
protected override ValueTask OnStoppingAsync() => new(dispatcher.InvokeAsync(root.StopActionsAsync));
/// <inheritdoc />
protected override ValueTask OnStoppedAsync() => new(dispatcher.InvokeAsync(root.DetachUiAsync));
}返回结果与关闭
由 RegisterGodotDialog 登记对话框工厂,将动作和上下文传入视图。确认按钮完成 bool 结果,关闭按钮结束当前对话框。宿主退出时先停止 UI 再退出树。
csharp
// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using System;
using System.Threading.Tasks;
using GFramework.Game.Abstractions.UI;
using Godot;
namespace GodotIntegration.GettingStarted;
/// <summary>Completes its typed result through a host-observed response that can run while the opener waits.</summary>
public partial class QuickStartConfirm : CenterContainer
{
private string _question = string.Empty;
private UiDialogContext<bool> _context = null!;
private Action<Func<Task>> _run = null!;
/// <summary>Receives the dialog's own completion context before entering the tree.</summary>
/// <param name="question">Opening argument.</param>
/// <param name="context">The owning UI instance.</param>
/// <param name="run">Observed response admission.</param>
public void Configure(string question, UiDialogContext<bool> context, Action<Func<Task>> run)
{
_question = question;
_context = context;
_run = run;
}
/// <inheritdoc />
public override void _Ready()
{
GetNode<Label>("Panel/Content/Question").Text = _question;
GetNode<Button>("Panel/Content/Accept").Pressed += () => _run(() => _context.CompleteAsync(result: true));
GetNode<Button>("Panel/Content/Cancel").Pressed += () => _run(() => _context.CloseAsync());
}
}自检
bash
godot --headless --path Samples/GodotIntegration res://GettingStarted/QuickStart.tscn -- --quickstart-smoke自检覆盖页面绑定、场景登记失败后的实例清理、确认、取消及关闭。接下来可运行完整小游戏。