user-interface - 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" ]
解决方案
将 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 内。
推荐阅读
- python - 在不使用 python name.py 和 ./name 的情况下运行 Python 代码
- swift - Swift for TensorFlow `错误:无法查找符号:TensorFlow.Tensor 的类型元数据访问器`
- kdb - 列出不执行自动展开的附加运算符
- javascript - javascript函数不采取形式价值?
- java - 每次运行应用程序时都会遇到这个问题: java.lang.NoClassDefFoundError: Class not found using the boot class loader; 没有可用的堆栈
- regex - 查询所有符合特定条件的
- html - 上边距拉动顶部元素
- python - 尽管在函数中生成,为什么我的绘图标签会发生变化?
- haskell - 在 Haskell 中解析文本时间戳的文本友好方式
- android - 如何将生成的 Dagger 模块添加到组件