首页 > 解决方案 > Godot如何使标签上的文本居中?

问题描述

我刚刚创建了一个新的 Godot 项目并创建了一个带有文本的标签。即使使用 Align: center 运行游戏时文本仍保持在左上角。

标签.tscn

[gd_scene format=2]

[node name="Label" type="Label" index="0"]

anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_right = 40.0
margin_bottom = 14.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 2
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 4
text = "heylab"
align = 1
valign = 1
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1
_sections_unfolded = [ "Anchor" ]

标签: user-interfacegodot

解决方案


将 Align 和 Valign 设置为 Center 后,您必须调整锚点和边距。你有一些不同的选择来做到这一点。

  • 在布局中选择“中心”将通过将锚点调整为 0.5(屏幕中心)来使标签居中,并计算边距,以便 Rect 居中(不改变其大小)。

  • 在Layout中选择“Full Rect”会将anchor设置为(0,0,1,1,即全屏),margins为0,并且会改变你的Label节点的Rect,这样节点就会填满屏幕。

当您选择控制节点(标签、容器等)时,工具栏中会出现布局按钮。 在 Godot 3 中显示布局按钮的屏幕截图

Obs.:最好先创建一个 Container 节点,将 Container 节点设置为 Full Rect,然后为您的标签创建子节点。您的锚点设置在父级的 Rect 内。


推荐阅读