问题描述
我有一个简单的 bevy
测试应用,它在左上角显示一组立方体和一个 FPS 计数器。 3d 场景可以缩放和旋转。
旧代码块 (bevy = 0.4.0
):
commands.spawn(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0,8.0,4.0)),..Default::default()
})
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.0,0.0,10 as f32 * 1.25))
.looking_at(Vec3::default(),Vec3::unit_y()),..Default::default()
})
.with(OrbitCamera::new(0.0,10 as f32 * 1.25,Vec3::zero()));
commands.spawn(CameraUiBundle::default())
// texture
.spawn(TextBundle {
transform: Transform::from_translation(Vec3::new(0.0,0.0)),style: Style {
align_self: AlignSelf::FlexEnd,..Default::default()
},text: Text {
value: " FPS:".to_string(),font: asset_server.load("fonts/FiraSans-Bold.ttf"),style: TextStyle {
font_size: 20.0,color: Color::WHITE,..Default::default()
},},..Default::default()
})
.with(FpsText);
我在单独的相机中生成 TextBundle
的原因是我希望它是静止的并且不对任何相机变换做出反应。
我尝试在 bevy = 0.5.0
中复制这种行为,如下所示:
commands.spawn_bundle(LightBundle {
transform: Transform::from_translation(Vec3::new(4.0,..Default::default()
});
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_translation(Vec3::new(0.0,10 as f32 * 1.25)).looking_at(Vec3::default(),Vec3::Y),..Default::default()
})
.insert(OrbitCamera::new(0.0,Vec3::ZERO));
commands.spawn_bundle(UiCameraBundle::default())
.insert_bundle(Text2dBundle {
text: Text::with_section(
" FPS:",TextStyle {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),font_size: 20.0,TextAlignment {
vertical: VerticalAlign::Top,horizontal: HorizontalAlign::Left,),..Default::default()
})
.insert(FpsText);
一切都再次按预期工作,除了 FPS 文本不再位于窗口的左上角,并且现在也像场景中的立方体一样旋转和缩放(我不希望发生这种情况)。
如何复制 bevy = 0.5.0
中的旧行为,以便将文本呈现为固定叠加层?
可以在 Github here (0.4.0) 和 here (0.5.0) 上找到这两个版本的完整代码。
解决方法
Text2dBundle 与常规相机相关联。如果您希望文本与 UI 摄像头相关联,则需要改用 TextBundle。您还需要在 TextBundle 上设置 Style 组件,以控制其位置,使其在屏幕上可见。
你的 0.4 代码片段成功地使用了 TextBundle,所以这只是需要更忠实地移植。