godot-csharp
在 Godot 里写 C# 时用。partial class 与源码生成器、[Export] [Signal] [GlobalClass] [Tool]、信号连接与断开、SignalName PropertyName 与 StringName 缓存、Variant 与 Godot.Collections、IsInstanceValid、async ToSignal、每帧分配。C# scripting in Godot.
- License 许可
- MIT
- 最近更新
- 2026-11-10
- Stars
- ★ 3
- Forks
- 0
01 前言 Front Matter
- name
- godot-csharp
- description
- 在 Godot 里写 C# 时用。partial class 与源码生成器、[Export] [Signal] [GlobalClass] [Tool]、信号连接与断开、SignalName PropertyName 与 StringName 缓存、Variant 与 Godot.Collections、IsInstanceValid、async ToSignal、每帧分配。C# scripting in Godot.
02 资源文件 2
- 参考文档references/gdscript-csharp.md8.1 KB
- 参考文档references/runtime.md12.8 KB
03 SKILL.md 正文
Godot C# 写法
C# 脚本的通用规则:源码生成器、命名映射、导出、信号、StringName、async。集合与对象生命周期见 references/runtime.md。各领域示例的 C# 写法在对应 skill 的 references/csharp.md。
范围
| 不含 | 去哪 |
|---|---|
| 各领域示例的 C# 写法 | 对应 skill 的 references/csharp.md |
| GDScript 写法 | godot-gdscript |
| 卡顿排查 | godot-performance |
[Tool] 节点在编辑器里显示与存盘 |
godot-scene-authoring |
先查内置
| 需求 | 用这个 | 别自己写 |
|---|---|---|
| 数学与随机 | Mathf.Lerp、Mathf.MoveToward、GD.Randf、GD.RandRange |
来回转 double 的 System.Math 调用 |
| 取节点 | GetNode<T>("%Name")、[Export] 引用、GetNodeOrNull<T> |
属性里或 _Process 里每次 GetNode() |
| 信号 | 生成的事件 Pressed += OnPressed,自定义用 [Signal] |
Connect("pressed", ...) 字符串 |
| 名字常量 | Node2D.PropertyName.Position、SignalName.Xxx、MethodName.Xxx |
每次调用传字符串字面量 |
| 等信号与延时 | await ToSignal(GetTree().CreateTimer(1.0), SceneTreeTimer.SignalName.Timeout) |
Task.Delay,不跟 Engine.TimeScale 和节点生命周期 |
| 帧末调用 | CallDeferred(MethodName.Foo),lambda 包成 Callable 再 CallDeferred() |
自建消息队列 |
| 后台计算 | WorkerThreadPool.AddTask、Task.Run 加 CallDeferred() 回主线程 |
自建 Thread 池 |
| 数据对象 | [GlobalClass] partial class X : Resource 加 [Export] |
JSON 加手写解析 |
| 输入判断 | 事件模式匹配、Input.GetVector |
手写正负键相减 |
| 日志 | GD.Print、GD.PushError、GD.PushWarning |
Console.WriteLine |
做法
类与源码生成器
using Godot;
[GlobalClass]
public partial class Turret : Node2D
{
[Export]
public PackedScene BulletScene { get; set; }
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
public float Cooldown { get; set; } = 0.5f;
[Export]
private Marker2D _muzzle;
private Label _label;
private float _timer;
public override void _Ready()
{
_label = GetNode<Label>("%Label");
}
public override void _Process(double delta)
{
_timer -= (float)delta;
}
}
| 规则 | 违反时 |
|---|---|
派生自 GodotObject 的类写 partial |
GD0001 |
外层类含此类的嵌套类时外层也 partial |
GD0002 |
[GlobalClass] 类不能是泛型 |
GD0402 |
挂到节点的脚本类名与 .cs 文件名一致,区分大小写 |
脚本挂不上 |
| 有无参构造,构造函数里不调依赖场景树的 API | 引擎创建实例失败 |
- 生成器给每个类补
PropertyName、MethodName、SignalName嵌套类 - 生成器同时生成注册、导出默认值、热重载的保存与恢复代码
- 自定义类的常量值是 C# 原名,引擎类的值是 snake_case
PropertyName含非静态、Variant 兼容的字段与属性,含 private 与只读MethodName含 Variant 兼容签名的方法,重载共用一个名字,泛型方法没有- 嵌套类继承基类的同名嵌套类,
Hud.PropertyName.Position也能用 - 改
[Export][Signal][Tool]后点编辑器右上角 Build,Inspector 才更新 [GlobalClass]让类出现在 Add Node 与 Create Resource 对话框,图标用[Icon("res://x.svg")][Tool]让脚本在编辑器里运行,用Engine.IsEditorHint()区分
命名映射
- 方法属性 snake_case 转大驼峰:
move_and_slide()对应MoveAndSlide(),getter setter 合成属性 Object对应GodotObject,RID对应Rid- 与属性同名的类内枚举加 Enum 后缀,如 Node.ProcessModeEnum
Vector2.RIGHT是只读属性Vector2.Right,不能用在case里- 接收字符串方法名的引擎 API 传 snake_case,优先用
Node.MethodName.AddChild常量 - 路径函数用
StringExtensions的GetBaseName(),System.IO.Path不认res:// - 全表见 references/gdscript-csharp.md
导出
#nullable enable
using Godot;
using Godot.Collections;
public partial class Spawner : Node
{
public enum Kind { Small, Big }
[Export]
public PackedScene Scene { get; set; } = null!;
[Export]
public Marker2D? Optional { get; set; }
[Export]
public Kind Size { get; set; } = Kind.Small;
[ExportGroup("Waves")]
[Export]
public Array<PackedScene> Waves { get; set; } = new();
[Export(PropertyHint.Enum, "Easy,Hard")]
public string Difficulty { get; set; } = "Easy";
}
| 情况 | 结果 |
|---|---|
List<T>、System.Collections.Generic.Dictionary |
不能导出,GD0102 |
| 静态、只读、只写、索引器 | GD0101 GD0103 GD0104 GD0105 |
非 Node 派生的类导出 Node 成员 |
GD0107 |
| 复杂初始化表达式 | 生成器得不到默认值 |
- 可导出:基本类型、
string、GodotObject派生类、Godot.Collections、byte[]Vector2[]等紧凑数组 - 字段与属性都可导出,任意访问级别
- 默认值由生成器分析初始化表达式得到,不执行 getter
- 启用 nullable 后,必有的引用写
= null!,可选的写? - 导出的 setter 在实例化时先于
_Ready()调用,子节点引用还是null [ExportToolButton("Bake")]4.4+,Callable类型的表达式体属性,类要[Tool]
信号
using Godot;
public partial class Health : Node
{
[Signal]
public delegate void ChangedEventHandler(int current, int max);
[Signal]
public delegate void DiedEventHandler();
[Export]
public int Max { get; set; } = 100;
private int _current = 100;
public void Damage(int amount)
{
_current = Mathf.Max(0, _current - amount);
EmitSignalChanged(_current, Max);
if (_current == 0)
{
EmitSignalDied();
}
}
}
public partial class Hud : Control
{
[Export]
public Health Target { get; set; }
private Label _label;
public override void _Ready()
{
_label = GetNode<Label>("Label");
}
public override void _EnterTree()
{
Target.Changed += OnChanged;
}
public override void _ExitTree()
{
Target.Changed -= OnChanged;
}
private void OnChanged(int current, int max)
{
_label.Text = $"{current}/{max}";
}
}
- 委托名以 EventHandler 结尾
GD0201,返回voidGD0203,参数 Variant 兼容GD0202 - 生成的事件名去掉后缀,事件不能
Invoke() - 发射用生成的
EmitSignalChanged(a, b),sealed类里是 private,否则 protected EmitSignal(SignalName.X, a, b)每次分配params数组,写成EmitSignal(SignalName.X, [a, b])不分配- 引擎信号同样是事件:
button.Pressed += OnPressed
| 连接方式 | 接收者释放后 |
|---|---|
引擎信号 += 实例方法 |
按 Delegate.Target 自动断开 |
| 捕获了局部变量的 lambda | 不自动断开,回调访问接收者抛 ObjectDisposedException |
自定义信号 += |
不自动断开,只写进发射者的托管委托 |
Connect(SignalName.X, Callable.From(OnX)) |
回调是接收者实例方法时自动断开 |
- 不自动断开的在
_EnterTree()连接、_ExitTree()里-=,lambda 要保留委托引用 - OneShot 等标志用
Connect():(uint)GodotObject.ConnectFlags.OneShot - GDScript 定义的信号只能字符串连接:
Connect("sig", Callable.From(...)) - 信号参数是自定义数据时类要继承
GodotObject
StringName 缓存
字符串隐式转 StringName 等于 new StringName(string),每次新建带终结器的托管对象并调一次原生,没有缓存。
using Godot;
public partial class Flash : Sprite2D
{
private static readonly StringName s_amount = "flash_amount";
private ShaderMaterial _material;
public override void _Ready()
{
_material = (ShaderMaterial)Material;
}
public override void _Process(double delta)
{
_material.SetShaderParameter(s_amount, 0.5f);
if (Input.IsActionJustPressed(Actions.Jump))
{
Modulate = Colors.White;
}
}
}
public static class Actions
{
public static readonly StringName Jump = "jump";
}
- 引擎名字用生成常量:
Node2D.PropertyName.Position、Button.SignalName.Pressed、Node.MethodName.AddChild - 输入动作、着色器参数、动画名、分组名、
Call()Get()Set()的名字放static readonly StringName - 名字只拼一次存起来,
NodePath同理
async 与线程
- 等信号用
await ToSignal(obj, SignalName.X),返回后先查IsInsideTree() - 没人
await的Task异常不打印,入口try/catch并GD.PushError - 线程里碰场景树只能
CallDeferred() - continuation 在哪个线程、等待对象被释放时的行为见 references/runtime.md
陷阱
| 现象 | 做法 |
|---|---|
Vector2 * delta 编译不过 |
delta 是 double,写 (float)delta |
Position.X = 1 报 CS1612 |
Position = Position with { X = 1 } |
GetNode<T>() 抛 InvalidCastException |
类型不符,可能为空用 GetNodeOrNull<T>() 或 is 模式匹配 |
每帧有 StringName NodePath 分配 |
字符串隐式转换,改成缓存字段 |
| 热重载后字段回到初始值 | 只读、init、静态、List<T> 不恢复,实现 ISerializationListener 重建 |
| 静态字段指向已释放对象 | 静态字段只放纯 C# 数据与 StringName |
| 程序集卸载失败 | 后台 Task、Thread、字段里的 Callable 在 _ExitTree() 里取消清理 |
| 把 EditorInterface 当静态类调用编译不过 | C# 里用它的 Singleton 属性 |
_Process 里卡顿 |
每帧分配与跨界开销,见 references/runtime.md |
参考
- references/gdscript-csharp.md:GDScript 与 C# 常用写法对照
- references/runtime.md:集合与 Variant、对象生命周期、async、输入事件、热重载、线程、跨界开销、每帧分配、托管泄漏排查
- https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_basics.html
- https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_differences.html
- https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_signals.html
- https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_collections.html
- https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_exports.html
- https://docs.godotengine.org/en/stable/tutorials/scripting/cross_language_scripting.html
04 同仓库更多 Skills
godot-ui
写菜单 HUD 对话框 血条 设置界面 本地化 分辨率适配 UI 导航时用,做设置即时生效 窗口模式 全屏 窗口尺寸确认还原 失焦暂停时也用。Control 布局 Container anchors、项目级 Theme 统一字体、StyleBox、ProgressBar 血条、RichTextLabel、多分辨率 stretch aspect、tr() 本地化、手柄焦点导航、mouse_filter 吃点击、CanvasLayer HUD、弹窗 拖放。UI layout, theme, localization, settings menu, fullscreen, pause on focus loss.
godot-shaders-vfx
做受击闪白 受伤变白 命中闪烁 溶解 描边,或从 GDScript 与 C# 改 .gdshader 的 shader 参数 SetInstanceShaderParameter set_shader_parameter 时先用,多个敌人共用一份材质各自闪白也归这里。也管内置材质与 shader 的取舍、instance uniform、屏幕与深度纹理、GPUParticles 粒子、后处理顺序。shaders, hit flash, particles, post-processing.
godot-scene-authoring
在 Godot 4 里新做关卡、房间、HUD、菜单、NPC、道具、背景时用,决定内容摆进 .tscn 还是代码运行时搭。新建项目第一个场景,往已有项目加场景,打开场景一片空看不到东西,美术策划要在编辑器里直接看和调,自定义显示节点在编辑器里不显示,@tool 节点把材质 z_index 写进 tscn,确定性模拟 联机 服务器要读场景里摆的位置。先自查项目是摆在场景里还是运行时生成,再按结果新做或迁移。脚本生成 tscn、@tool 自绘节点、预制场景、_validate_property、internal 子节点、NOTIFICATION_EDITOR_PRE_SAVE、C# 重载。level authoring, WYSIWYG, prefab, data-driven, bake on save, empty scene, audit.
godot-scene-architecture
拆分 Godot 4 场景与组织节点时用。PackedScene 组件复用、节点引用 @onready @export %UniqueName、信号与直接调用的选择、分组 call_group、Autoload 与普通节点、组合与继承、状态机、暂停 process_mode、queue_free 与 is_instance_valid、_ready 顺序、场景切换。scene tree architecture, signals, autoload.
godot-resources-data
定义游戏数据表 配置 存档 资源加载时用。自定义 Resource class_name @export .tres 与 C# [GlobalClass]、JSON 与 Resource 怎么选、导入时把 JSON CSV 转成 Resource、资源共享与 duplicate local_to_scene、load preload 与线程加载进度条、user:// 存档、导出后读不到 res:// 目录。game data, custom resource, save game.
godot-performance
排查卡顿 掉帧 帧率低 内存增长时用,也用于几百个怪物 子弹同屏、CharacterBody 拥挤变慢、每帧重建网格、线程池只用上两三个核、C# 跨界开销。先测再改 Profiler Monitors。不降画面与数量的内置解法 管理器统一驱动、MultiMesh、Server、对象池、空间哈希、WorkerThreadPool 分块、网格局部更新、物理插值、线程加载、着色器编译卡顿、每帧分配。Godot performance, stutter, many entities.