ui/text example: Use a unit component to identify the target Text (#612)

This commit is contained in:
Zach Gotsch 2020-10-05 12:07:14 -07:00 committed by GitHub
parent 125afb41ac
commit d61a1735e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,8 +13,11 @@ fn main() {
.run(); .run();
} }
fn text_update_system(diagnostics: Res<Diagnostics>, mut query: Query<&mut Text>) { // A unit struct to help identify the FPS UI component, since there may be many Text components
for mut text in &mut query.iter() { struct FpsText;
fn text_update_system(diagnostics: Res<Diagnostics>, mut query: Query<(&mut Text, &FpsText)>) {
for (mut text, _tag) in &mut query.iter() {
if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) { if let Some(fps) = diagnostics.get(FrameTimeDiagnosticsPlugin::FPS) {
if let Some(average) = fps.average() { if let Some(average) = fps.average() {
text.value = format!("FPS: {:.2}", average); text.value = format!("FPS: {:.2}", average);
@ -43,5 +46,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
}, },
}, },
..Default::default() ..Default::default()
}); })
.with(FpsText);
} }