CodeEdit
2026/4/14大约 2 分钟
最后同步日期:2026-04-15 | Godot 官方原文 — CodeEdit
CodeEdit
节点继承关系
继承链:Node → CanvasItem → Control → TextEdit → CodeEdit
定义
继承自 TextEdit,专门用来编辑代码。它提供了代码编辑器常见的功能:行号显示、代码补全提示、括号匹配高亮等。就像 Godot 编辑器里那个写代码的区域一样。
使用频率:★ 基本用不到(做游戏内置编辑器时才用)
节点用途
- 游戏内置的控制台/脚本编辑器
- 自定义的代码输入界面
使用场景
- 游戏内置的 Lua 脚本编辑器
- 关卡编辑器中嵌入的逻辑编辑
常用节点搭配
- 搭配
ScrollContainer实现可滚动的代码区域 - 搭配
VBoxContainer做编辑器布局
生效必备素材/资源
无需特殊资源。可选:语法高亮主题配置。
节点属性与信号
自有属性
| 属性 | 类型 | 默认值 | 继承自 | 说明 |
|---|---|---|---|---|
syntax_highlighter | SyntaxHighlighter | null | — | 语法高亮器 |
code_completion_enabled | bool | true | — | 是否启用代码补全 |
line_numbers_background_visible | bool | true | — | 是否显示行号背景 |
gutter_visible | bool | true | — | 是否显示行号栏 |
继承自 TextEdit
| 属性 | 类型 | 默认值 | 继承自 | 说明 |
|---|---|---|---|---|
text | string | "" | TextEdit | 编辑器中的全部文字 |
editable | bool | true | TextEdit | 是否可编辑 |
wrap_mode | 枚举 | None | TextEdit | 是否自动换行 |
信号
| 信号 | 触发时机 | 参数 |
|---|---|---|
text_changed | 文本内容发生变化时 | 无 |
code_completion_requested | 请求代码补全时 | 无 |
常用方法
| 方法 | 说明 |
|---|---|
| 继承 TextEdit 所有方法 | — |
SetSyntaxHighlighter(highlighter) | 设置语法高亮器 |
代码示例
C
using Godot;
public partial class ConsoleEditor : Control
{
public override void _Ready()
{
var codeEdit = new CodeEdit();
codeEdit.PlaceholderText = "// 在这里输入代码...";
codeEdit.CustomMinimumSize = new Vector2(500, 300);
codeEdit.Position = new Vector2(50, 50);
codeEdit.Text = "// 欢迎使用内置脚本编辑器\nfunc _ready():\n pass";
codeEdit.SetSyntaxHighlighter(null); // 可配置高亮器
AddChild(codeEdit);
}
}GDScript
extends Control
func _ready():
var code_edit = CodeEdit.new()
code_edit.placeholder_text = "// 在这里输入代码..."
code_edit.custom_minimum_size = Vector2(500, 300)
code_edit.position = Vector2(50, 50)
code_edit.text = "// 欢迎使用内置脚本编辑器\nfunc _ready():\n pass"
code_edit.syntax_highlighter = null # 可配置高亮器
add_child(code_edit)