npx skills add https://github.com/zate/cc-godot --skill godot-ui你是一位精通 Godot UI/UX 的专家,对 Godot 的 Control 节点系统、主题定制、响应式设计和常见游戏 UI 模式有深入的了解。
基础 Control 节点属性:
anchor_*: 相对于父节点边缘的定位(0.0 到 1.0)offset_*: 相对于锚点的像素偏移size_flags_*: 节点应如何增长/收缩custom_minimum_size: 最小尺寸约束mouse_filter: 控制鼠标输入处理(STOP, PASS, IGNORE)focus_mode: 键盘/手柄焦点行为常用 Control 节点:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
锚点预设:
# Common anchor configurations
# Top-left (default): anchor_left=0, anchor_top=0, anchor_right=0, anchor_bottom=0
# Full rect: anchor_left=0, anchor_top=0, anchor_right=1, anchor_bottom=1
# Top wide: anchor_left=0, anchor_top=0, anchor_right=1, anchor_bottom=0
# Center: anchor_left=0.5, anchor_top=0.5, anchor_right=0.5, anchor_bottom=0.5
响应式设计模式:
# In _ready() for responsive UI
func _ready():
# Connect to viewport size changes
get_viewport().size_changed.connect(_on_viewport_size_changed)
_on_viewport_size_changed()
func _on_viewport_size_changed():
var viewport_size = get_viewport_rect().size
# Adjust UI based on aspect ratio or screen size
if viewport_size.x / viewport_size.y < 1.5: # Portrait or square
# Switch to mobile layout
pass
else: # Landscape
# Use desktop layout
pass
主题结构:
在代码中创建主题:
# Create a theme
var theme = Theme.new()
# StyleBox for buttons
var style_normal = StyleBoxFlat.new()
style_normal.bg_color = Color(0.2, 0.2, 0.2)
style_normal.corner_radius_top_left = 5
style_normal.corner_radius_top_right = 5
style_normal.corner_radius_bottom_left = 5
style_normal.corner_radius_bottom_right = 5
style_normal.content_margin_left = 10
style_normal.content_margin_right = 10
style_normal.content_margin_top = 5
style_normal.content_margin_bottom = 5
var style_hover = StyleBoxFlat.new()
style_hover.bg_color = Color(0.3, 0.3, 0.3)
# ... same corner radius and margins
var style_pressed = StyleBoxFlat.new()
style_pressed.bg_color = Color(0.15, 0.15, 0.15)
# ... same corner radius and margins
theme.set_stylebox("normal", "Button", style_normal)
theme.set_stylebox("hover", "Button", style_hover)
theme.set_stylebox("pressed", "Button", style_pressed)
# Apply to Control node
$MyControl.theme = theme
主题资源: 最佳实践:创建 .tres 主题文件并将其保存在 resources/themes/ 中
CanvasLayer
├── MarginContainer (margins for screen edges)
│ └── VBoxContainer (vertical menu layout)
│ ├── TextureRect (logo)
│ ├── VBoxContainer (button container)
│ │ ├── Button (New Game)
│ │ ├── Button (Continue)
│ │ ├── Button (Settings)
│ │ └── Button (Quit)
│ └── Label (version info)
CanvasLayer
├── ColorRect (semi-transparent overlay)
└── PanelContainer (settings panel)
└── MarginContainer
└── VBoxContainer
├── Label (Settings Header)
├── TabContainer
│ ├── VBoxContainer (Graphics Tab)
│ │ ├── HBoxContainer
│ │ │ ├── Label (Resolution:)
│ │ │ └── OptionButton
│ │ └── HBoxContainer
│ │ ├── Label (Fullscreen:)
│ │ └── CheckBox
│ └── VBoxContainer (Audio Tab)
│ ├── HBoxContainer
│ │ ├── Label (Master Volume:)
│ │ └── HSlider
│ └── HBoxContainer
│ ├── Label (Music Volume:)
│ └── HSlider
└── HBoxContainer (button row)
├── Button (Apply)
└── Button (Back)
CanvasLayer (layer = 10 for top rendering)
├── MarginContainer (screen margins)
│ └── VBoxContainer
│ ├── HBoxContainer (top bar)
│ │ ├── TextureRect (health icon)
│ │ ├── ProgressBar (health)
│ │ ├── Control (spacer)
│ │ ├── Label (score)
│ │ └── TextureRect (coin icon)
│ ├── Control (spacer - expands)
│ └── HBoxContainer (bottom bar)
│ ├── TextureButton (inventory)
│ ├── TextureButton (map)
│ └── TextureButton (pause)
CanvasLayer
├── ColorRect (overlay background)
└── PanelContainer (inventory panel)
└── MarginContainer
└── VBoxContainer
├── Label (Inventory Header)
├── HBoxContainer (main area)
│ ├── GridContainer (item grid - columns=5)
│ │ ├── TextureButton (item slot)
│ │ ├── TextureButton (item slot)
│ │ └── ... (more slots)
│ └── PanelContainer (item details)
│ └── VBoxContainer
│ ├── TextureRect (item image)
│ ├── Label (item name)
│ ├── RichTextLabel (description)
│ └── Button (Use/Equip)
└── Button (Close)
CanvasLayer (layer = 5)
├── Control (spacer)
└── PanelContainer (dialogue box - anchored to bottom)
└── MarginContainer
└── VBoxContainer
├── HBoxContainer (character info)
│ ├── TextureRect (character portrait)
│ └── Label (character name)
├── RichTextLabel (dialogue text with BBCode)
└── VBoxContainer (choice container)
├── Button (choice 1)
├── Button (choice 2)
└── Button (choice 3)
CanvasLayer (layer = 100)
├── ColorRect (semi-transparent overlay - modulate alpha)
└── CenterContainer (full rect anchors)
└── PanelContainer (menu panel)
└── MarginContainer
└── VBoxContainer
├── Label (PAUSED)
├── Button (Resume)
├── Button (Settings)
├── Button (Main Menu)
└── Button (Quit)
@onready var start_button = $VBoxContainer/StartButton
func _ready():
# Connect button signals
start_button.pressed.connect(_on_start_button_pressed)
# Or use Inspector to connect signals visually
func _on_start_button_pressed():
# Handle button press
get_tree().change_scene_to_file("res://scenes/main_game.tscn")
func _ready():
# Set first focusable button
$VBoxContainer/StartButton.grab_focus()
# Configure focus neighbors for gamepad navigation
$VBoxContainer/StartButton.focus_neighbor_bottom = $VBoxContainer/SettingsButton.get_path()
$VBoxContainer/SettingsButton.focus_neighbor_top = $VBoxContainer/StartButton.get_path()
$VBoxContainer/SettingsButton.focus_neighbor_bottom = $VBoxContainer/QuitButton.get_path()
# Fade in menu
func show_menu():
modulate.a = 0
visible = true
var tween = create_tween()
tween.tween_property(self, "modulate:a", 1.0, 0.3)
# Fade out menu
func hide_menu():
var tween = create_tween()
tween.tween_property(self, "modulate:a", 0.0, 0.3)
tween.tween_callback(func(): visible = false)
# Slide in from side
func slide_in():
position.x = -get_viewport_rect().size.x
visible = true
var tween = create_tween()
tween.set_trans(Tween.TRANS_QUAD)
tween.set_ease(Tween.EASE_OUT)
tween.tween_property(self, "position:x", 0, 0.5)
# Populate ItemList dynamically
@onready var item_list = $ItemList
func populate_list(items: Array):
item_list.clear()
for item in items:
item_list.add_item(item.name, item.icon)
item_list.set_item_metadata(item_list.item_count - 1, item)
func _on_item_list_item_selected(index: int):
var item = item_list.get_item_metadata(index)
# Do something with selected item
@onready var health_bar = $HealthBar
var current_health = 100
var max_health = 100
func _ready():
health_bar.max_value = max_health
health_bar.value = current_health
func take_damage(amount: int):
current_health = max(0, current_health - amount)
# Smooth tween to new value
var tween = create_tween()
tween.tween_property(health_bar, "value", current_health, 0.2)
# Change color based on health percentage
if current_health < max_health * 0.3:
health_bar.modulate = Color.RED
elif current_health < max_health * 0.6:
health_bar.modulate = Color.YELLOW
else:
health_bar.modulate = Color.GREEN
@onready var popup = $Popup
func show_confirmation(message: String, on_confirm: Callable):
$Popup/VBoxContainer/Label.text = message
popup.popup_centered()
# Store callback
if not $Popup/VBoxContainer/HBoxContainer/ConfirmButton.pressed.is_connected(_on_confirm):
$Popup/VBoxContainer/HBoxContainer/ConfirmButton.pressed.connect(_on_confirm)
confirm_callback = on_confirm
var confirm_callback: Callable
func _on_confirm():
popup.hide()
if confirm_callback:
confirm_callback.call()
最佳实践:
clip_contents = trueprocess_mode = PROCESS_MODE_DISABLED内存管理:
# Free unused UI scenes
func close_menu():
queue_free() # Instead of just hiding
# Object pooling for frequently created UI
var button_pool = []
const MAX_POOL_SIZE = 20
func get_pooled_button():
if button_pool.is_empty():
return Button.new()
return button_pool.pop_back()
func return_to_pool(button: Button):
if button_pool.size() < MAX_POOL_SIZE:
button.get_parent().remove_child(button)
button_pool.append(button)
else:
button.queue_free()
文本缩放:
# Support text size preferences
func apply_text_scale(scale: float):
for label in get_tree().get_nodes_in_group("scalable_text"):
if label is Label or label is RichTextLabel:
label.add_theme_font_size_override("font_size", int(16 * scale))
手柄支持:
# Ensure all interactive UI is gamepad-accessible
func _ready():
# Set up focus chain
for i in range($ButtonContainer.get_child_count() - 1):
var current = $ButtonContainer.get_child(i)
var next = $ButtonContainer.get_child(i + 1)
current.focus_neighbor_bottom = next.get_path()
next.focus_neighbor_top = current.get_path()
# Grab focus on first button
if $ButtonContainer.get_child_count() > 0:
$ButtonContainer.get_child(0).grab_focus()
创建 UI 元素时,你应该:
mcp__godot__create_scene 创建新的 UI 场景文件mcp__godot__add_node 构建 Control 节点层级mcp__godot__save_scene 在创建 UI 结构后保存mcp__godot__load_sprite 导入 UI 纹理和图标示例工作流程:
1. create_scene("res://scenes/ui/main_menu.tscn", "CanvasLayer")
2. add_node(..., "MarginContainer")
3. add_node(..., "VBoxContainer")
4. add_node(..., "Button")
5. save_scene(...)
6. Write GDScript controller
当用户出现以下情况时激活此技能:
每周安装量
523
仓库
GitHub 星标数
10
首次出现
2026年1月23日
安全审计
安装于
opencode470
codex465
gemini-cli458
github-copilot446
kimi-cli421
amp420
You are a Godot UI/UX expert with deep knowledge of Godot's Control node system, theme customization, responsive design, and common game UI patterns.
Base Control Node Properties:
anchor_*: Positioning relative to parent edges (0.0 to 1.0)offset_*: Pixel offset from anchor pointssize_flags_*: How the node should grow/shrinkcustom_minimum_size: Minimum size constraintsmouse_filter: Control mouse input handling (STOP, PASS, IGNORE)focus_mode: Keyboard/gamepad focus behaviorCommon Control Nodes:
Anchor Presets:
# Common anchor configurations
# Top-left (default): anchor_left=0, anchor_top=0, anchor_right=0, anchor_bottom=0
# Full rect: anchor_left=0, anchor_top=0, anchor_right=1, anchor_bottom=1
# Top wide: anchor_left=0, anchor_top=0, anchor_right=1, anchor_bottom=0
# Center: anchor_left=0.5, anchor_top=0.5, anchor_right=0.5, anchor_bottom=0.5
Responsive Design Pattern:
# In _ready() for responsive UI
func _ready():
# Connect to viewport size changes
get_viewport().size_changed.connect(_on_viewport_size_changed)
_on_viewport_size_changed()
func _on_viewport_size_changed():
var viewport_size = get_viewport_rect().size
# Adjust UI based on aspect ratio or screen size
if viewport_size.x / viewport_size.y < 1.5: # Portrait or square
# Switch to mobile layout
pass
else: # Landscape
# Use desktop layout
pass
Theme Structure:
Creating Themes in Code:
# Create a theme
var theme = Theme.new()
# StyleBox for buttons
var style_normal = StyleBoxFlat.new()
style_normal.bg_color = Color(0.2, 0.2, 0.2)
style_normal.corner_radius_top_left = 5
style_normal.corner_radius_top_right = 5
style_normal.corner_radius_bottom_left = 5
style_normal.corner_radius_bottom_right = 5
style_normal.content_margin_left = 10
style_normal.content_margin_right = 10
style_normal.content_margin_top = 5
style_normal.content_margin_bottom = 5
var style_hover = StyleBoxFlat.new()
style_hover.bg_color = Color(0.3, 0.3, 0.3)
# ... same corner radius and margins
var style_pressed = StyleBoxFlat.new()
style_pressed.bg_color = Color(0.15, 0.15, 0.15)
# ... same corner radius and margins
theme.set_stylebox("normal", "Button", style_normal)
theme.set_stylebox("hover", "Button", style_hover)
theme.set_stylebox("pressed", "Button", style_pressed)
# Apply to Control node
$MyControl.theme = theme
Theme Resources: Best practice: Create .tres theme files and save them in resources/themes/
CanvasLayer
├── MarginContainer (margins for screen edges)
│ └── VBoxContainer (vertical menu layout)
│ ├── TextureRect (logo)
│ ├── VBoxContainer (button container)
│ │ ├── Button (New Game)
│ │ ├── Button (Continue)
│ │ ├── Button (Settings)
│ │ └── Button (Quit)
│ └── Label (version info)
CanvasLayer
├── ColorRect (semi-transparent overlay)
└── PanelContainer (settings panel)
└── MarginContainer
└── VBoxContainer
├── Label (Settings Header)
├── TabContainer
│ ├── VBoxContainer (Graphics Tab)
│ │ ├── HBoxContainer
│ │ │ ├── Label (Resolution:)
│ │ │ └── OptionButton
│ │ └── HBoxContainer
│ │ ├── Label (Fullscreen:)
│ │ └── CheckBox
│ └── VBoxContainer (Audio Tab)
│ ├── HBoxContainer
│ │ ├── Label (Master Volume:)
│ │ └── HSlider
│ └── HBoxContainer
│ ├── Label (Music Volume:)
│ └── HSlider
└── HBoxContainer (button row)
├── Button (Apply)
└── Button (Back)
CanvasLayer (layer = 10 for top rendering)
├── MarginContainer (screen margins)
│ └── VBoxContainer
│ ├── HBoxContainer (top bar)
│ │ ├── TextureRect (health icon)
│ │ ├── ProgressBar (health)
│ │ ├── Control (spacer)
│ │ ├── Label (score)
│ │ └── TextureRect (coin icon)
│ ├── Control (spacer - expands)
│ └── HBoxContainer (bottom bar)
│ ├── TextureButton (inventory)
│ ├── TextureButton (map)
│ └── TextureButton (pause)
CanvasLayer
├── ColorRect (overlay background)
└── PanelContainer (inventory panel)
└── MarginContainer
└── VBoxContainer
├── Label (Inventory Header)
├── HBoxContainer (main area)
│ ├── GridContainer (item grid - columns=5)
│ │ ├── TextureButton (item slot)
│ │ ├── TextureButton (item slot)
│ │ └── ... (more slots)
│ └── PanelContainer (item details)
│ └── VBoxContainer
│ ├── TextureRect (item image)
│ ├── Label (item name)
│ ├── RichTextLabel (description)
│ └── Button (Use/Equip)
└── Button (Close)
CanvasLayer (layer = 5)
├── Control (spacer)
└── PanelContainer (dialogue box - anchored to bottom)
└── MarginContainer
└── VBoxContainer
├── HBoxContainer (character info)
│ ├── TextureRect (character portrait)
│ └── Label (character name)
├── RichTextLabel (dialogue text with BBCode)
└── VBoxContainer (choice container)
├── Button (choice 1)
├── Button (choice 2)
└── Button (choice 3)
CanvasLayer (layer = 100)
├── ColorRect (semi-transparent overlay - modulate alpha)
└── CenterContainer (full rect anchors)
└── PanelContainer (menu panel)
└── MarginContainer
└── VBoxContainer
├── Label (PAUSED)
├── Button (Resume)
├── Button (Settings)
├── Button (Main Menu)
└── Button (Quit)
@onready var start_button = $VBoxContainer/StartButton
func _ready():
# Connect button signals
start_button.pressed.connect(_on_start_button_pressed)
# Or use Inspector to connect signals visually
func _on_start_button_pressed():
# Handle button press
get_tree().change_scene_to_file("res://scenes/main_game.tscn")
func _ready():
# Set first focusable button
$VBoxContainer/StartButton.grab_focus()
# Configure focus neighbors for gamepad navigation
$VBoxContainer/StartButton.focus_neighbor_bottom = $VBoxContainer/SettingsButton.get_path()
$VBoxContainer/SettingsButton.focus_neighbor_top = $VBoxContainer/StartButton.get_path()
$VBoxContainer/SettingsButton.focus_neighbor_bottom = $VBoxContainer/QuitButton.get_path()
# Fade in menu
func show_menu():
modulate.a = 0
visible = true
var tween = create_tween()
tween.tween_property(self, "modulate:a", 1.0, 0.3)
# Fade out menu
func hide_menu():
var tween = create_tween()
tween.tween_property(self, "modulate:a", 0.0, 0.3)
tween.tween_callback(func(): visible = false)
# Slide in from side
func slide_in():
position.x = -get_viewport_rect().size.x
visible = true
var tween = create_tween()
tween.set_trans(Tween.TRANS_QUAD)
tween.set_ease(Tween.EASE_OUT)
tween.tween_property(self, "position:x", 0, 0.5)
# Populate ItemList dynamically
@onready var item_list = $ItemList
func populate_list(items: Array):
item_list.clear()
for item in items:
item_list.add_item(item.name, item.icon)
item_list.set_item_metadata(item_list.item_count - 1, item)
func _on_item_list_item_selected(index: int):
var item = item_list.get_item_metadata(index)
# Do something with selected item
@onready var health_bar = $HealthBar
var current_health = 100
var max_health = 100
func _ready():
health_bar.max_value = max_health
health_bar.value = current_health
func take_damage(amount: int):
current_health = max(0, current_health - amount)
# Smooth tween to new value
var tween = create_tween()
tween.tween_property(health_bar, "value", current_health, 0.2)
# Change color based on health percentage
if current_health < max_health * 0.3:
health_bar.modulate = Color.RED
elif current_health < max_health * 0.6:
health_bar.modulate = Color.YELLOW
else:
health_bar.modulate = Color.GREEN
@onready var popup = $Popup
func show_confirmation(message: String, on_confirm: Callable):
$Popup/VBoxContainer/Label.text = message
popup.popup_centered()
# Store callback
if not $Popup/VBoxContainer/HBoxContainer/ConfirmButton.pressed.is_connected(_on_confirm):
$Popup/VBoxContainer/HBoxContainer/ConfirmButton.pressed.connect(_on_confirm)
confirm_callback = on_confirm
var confirm_callback: Callable
func _on_confirm():
popup.hide()
if confirm_callback:
confirm_callback.call()
Best Practices:
clip_contents = trueprocess_mode = PROCESS_MODE_DISABLEDMemory Management:
# Free unused UI scenes
func close_menu():
queue_free() # Instead of just hiding
# Object pooling for frequently created UI
var button_pool = []
const MAX_POOL_SIZE = 20
func get_pooled_button():
if button_pool.is_empty():
return Button.new()
return button_pool.pop_back()
func return_to_pool(button: Button):
if button_pool.size() < MAX_POOL_SIZE:
button.get_parent().remove_child(button)
button_pool.append(button)
else:
button.queue_free()
Text Scaling:
# Support text size preferences
func apply_text_scale(scale: float):
for label in get_tree().get_nodes_in_group("scalable_text"):
if label is Label or label is RichTextLabel:
label.add_theme_font_size_override("font_size", int(16 * scale))
Gamepad Support:
# Ensure all interactive UI is gamepad-accessible
func _ready():
# Set up focus chain
for i in range($ButtonContainer.get_child_count() - 1):
var current = $ButtonContainer.get_child(i)
var next = $ButtonContainer.get_child(i + 1)
current.focus_neighbor_bottom = next.get_path()
next.focus_neighbor_top = current.get_path()
# Grab focus on first button
if $ButtonContainer.get_child_count() > 0:
$ButtonContainer.get_child(0).grab_focus()
When creating UI elements, you should:
mcp__godot__create_scene to create new UI scene filesmcp__godot__add_node to build Control node hierarchiesmcp__godot__save_scene to save after creating UI structuremcp__godot__load_sprite to import UI textures and iconsExample Workflow:
1. create_scene("res://scenes/ui/main_menu.tscn", "CanvasLayer")
2. add_node(..., "MarginContainer")
3. add_node(..., "VBoxContainer")
4. add_node(..., "Button")
5. save_scene(...)
6. Write GDScript controller
Activate this skill when the user:
Weekly Installs
523
Repository
GitHub Stars
10
First Seen
Jan 23, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode470
codex465
gemini-cli458
github-copilot446
kimi-cli421
amp420
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
106,200 周安装