godot-ui-control
它能做什么
该技能讲解如何使用 Control 节点在 Godot 中构建游戏菜单、抬头显示和设置界面。适合希望界面适配多种屏幕尺寸的开发者。
适用场景
- ✓当你在 Godot 中布局菜单、HUD、物品栏或设置界面时。
- ✓当你希望窗口尺寸变化时界面能够自动调整时。
- ✓当玩家需要用键盘或手柄在菜单中移动时。
技能链接
https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/godot/godot-ui-control/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请在 Godot 中制作一个暂停菜单,包含“继续”和“退出”按钮,并且也能用手柄操作。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能讲解用于调整尺寸的锚点、自动排列元素的容器、保持样式一致的主题,以及用于手柄导航的焦点设置。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Godot UI / Control nodes (4.x)
Lay out responsive UI with Control anchors and Container nodes, style it with a
Theme, and make it navigable by keyboard and gamepad. Targets Godot 4.7.
When to use
- Use when building HUDs, menus, inventories, dialog boxes, or settings screens with
Control-derived nodes; arranging UI that adapts to window size; theming; or wiring focus navigation for controller/keyboard.
When not to use: in-world 2D nodes (Node2D/sprites) → godot-nodes-scenes;
animating UI transitions → godot-animation (Tween); genre UIs like card hands →
card-game/visual-novel. For full input rebinding → input-systems.
Core workflow
- Use
Controlnodes for UI, notNode2D. Controls have a rect (position + size), anchors, and participate in focus/theming. - Anchor for responsiveness. Anchors are fractions (0–1) of the parent rect that the Control's edges stick to. Use the editor's Layout presets (Top-Left, Full Rect, Center, etc.) instead of hand-placing pixels.
- Let Containers position children. Put children in a
VBoxContainer,HBoxContainer,GridContainer,MarginContainer, etc. — the container sets their position/size; you control flow withsize_flags. Don't set child anchors inside a container (it overrides them). - Style with a
Theme. Assign aThemeresource on a top Control; children inherit it. Override per-node with theme overrides only when necessary. - Wire focus so gamepad/keyboard can move between buttons; set a default focused control and define neighbors or rely on auto-neighbor.
- Connect signals (
pressed,toggled,text_submitted,value_changed).
Patterns
1. Responsive layout with anchors (code form)
extends Control
func _ready() -> void:
# Stretch this panel to fill its parent (the editor's "Full Rect" preset).
# Setting only anchors (or `anchors_preset`) keeps the current offsets, so the
# control keeps its old rect; this call moves anchors AND resets offsets.
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
# Manual equivalent: anchor_left = 0; anchor_top = 0; anchor_right = 1;
# anchor_bottom = 1, then set all four offset_* to 0.
2. A menu built from containers + button signals
extends VBoxContainer # children stack vertically, auto-sized
func _ready() -> void:
for child in get_children():
if child is Button:
child.pressed.connect(_on_button_pressed.bind(child.name))
# Give the first button focus so a gamepad can navigate immediately.
if get_child_count() > 0:
(get_child(0) as Control).grab_focus()
func _on_button_pressed(which: StringName) -> void:
match which:
"PlayButton": get_tree().change_scene_to_file("res://game.tscn")
"QuitButton": get_tree().quit()
3. Size flags: make one child expand to fill leftover space
# In a HBoxContainer: a label on the left, a spacer that eats remaining width.
func _ready() -> void:
$Label.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
$Spacer.size_flags_horizontal = Control.SIZE_EXPAND_FILL # grows to fill
4. Theme override for one node (without a full Theme resource)
func _ready() -> void:
# Per-node overrides: use add_theme_* (type-specific setters).
$Title.add_theme_font_size_override("font_size", 32)
$Title.add_theme_color_override("font_color", Color.GOLD)
$Panel.add_theme_stylebox_override("panel", preload("res://ui/panel.stylebox.tres"))
Pitfalls
- Mixing manual position with Containers. A child of a
Containercannot set its own position/anchors — the container owns layout. To free-place, take the node out of the container or use a plainControl/PanelContainerwrapper. To nudge, rotate or scale a container child (e.g. a hover pop) in 4.7+, setoffset_transform_enabled = trueand useoffset_transform_position/_rotation/_scale; the container leaves those alone. - Anchors vs offsets. Anchors are fractions of the parent; offsets are pixel deltas from the anchored point. Set anchors via presets, then nudge with offsets. Setting only position while anchors are at 0 makes UI not scale with the window.
Node2Dfor UI. Buttons/labels parented under aNode2Dwon't theme or take focus correctly. Keep UI under aCanvasLayer/Controlsubtree.- Focus lost on gamepad. If
常见问题
什么是 Control 节点?
Control 节点是 Godot 中用于用户界面元素的类型,与游戏世界中的对象不同。
Godot 中的 Theme 是什么?
Theme 是保存下来的一组样式规则,可以应用到整个界面。
它涵盖输入重新绑定吗?
不完全涵盖。完整的输入重新绑定由另一份输入技能负责。
详情
作者的原始描述
Build Godot 4.7 user interfaces with Control nodes: anchors and offsets for responsive layout, Container nodes (VBox/HBox/Grid/Margin) for automatic arrangement, Theme resources for consistent styling, and keyboard/gamepad focus navigation. Use when laying out a HUD, menu, or UI in a Godot project, working with Control/Container nodes, anchors, themes, or focus in a .tscn.
热度
- GitHub 星标
- 6.6k
- 排名
- #13831
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/28
评论
- …