我有一个简单的bevy测试应用程序,它显示了一个立方体阵列和一个FPS计数器在左上角。可以缩放和旋转3d场景。
我最近尝试将以下代码升级到bevy = 0.5.0。
旧代码块(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, 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, 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, 8.0, 4.0)),
..Default::default()
});
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 10 as f32 * 1.25)).looking_at(Vec3::default(), Vec3::Y),
..Default::default()
})
.insert(OrbitCamera::new(0.0, 0.0, 10 as f32 * 1.25, 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,
color: Color::WHITE,
},
TextAlignment {
vertical: VerticalAlign::Top,
horizontal: HorizontalAlign::Left,
},
),
..Default::default()
})
.insert(FpsText);一切都像预期一样工作,除了FPS文本不再位于窗口的左上角,并且现在也像场景中的立方体一样旋转和缩放(这是我不希望发生的)。
如何在bevy = 0.5.0中复制旧的行为,以便将文本呈现为固定覆盖?
发布于 2021-04-30 06:52:46
Text2dBundle与常规摄像头关联。如果想要与UI相机关联的文本,则需要改用TextBundle。您还需要在TextBundle上设置样式组件,以便控制其位置,使其在屏幕上可见。
您的bevy 0.4代码片段已经成功地使用了TextBundle,所以这只需要更忠实地移植即可。
https://stackoverflow.com/questions/67031681
复制相似问题