many_buttons --respawn arg (#16390)

# Objective

To capture the performance impact of removing and adding UI nodes add a
`respawn` commandline argument to the `many_buttons` stress test example
that despawns the existing UI layout and then spawns a new layout to
replace it every frame.

## Testing

To run the example with the new changes use:
```cargo run --example many_buttons --release -- --respawn```
This commit is contained in:
ickshonpe 2024-11-14 19:50:33 +00:00 committed by GitHub
parent 4eaebd4608
commit 2d91a6fc39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -42,6 +42,10 @@ struct Args {
/// use the grid layout model /// use the grid layout model
#[argh(switch)] #[argh(switch)]
grid: bool, grid: bool,
/// at the start of each frame despawn any existing UI nodes and spawn a new UI tree
#[argh(switch)]
respawn: bool,
} }
/// This example shows what happens when there is a lot of buttons on screen. /// This example shows what happens when there is a lot of buttons on screen.
@ -52,6 +56,8 @@ fn main() {
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
let args = Args::from_args(&[], &[]).unwrap(); let args = Args::from_args(&[], &[]).unwrap();
warn!(include_str!("warning_string.txt"));
let mut app = App::new(); let mut app = App::new();
app.add_plugins(( app.add_plugins((
@ -72,6 +78,10 @@ fn main() {
}) })
.add_systems(Update, (button_system, set_text_colors_changed)); .add_systems(Update, (button_system, set_text_colors_changed));
app.add_systems(Startup, |mut commands: Commands| {
commands.spawn(Camera2d);
});
if args.grid { if args.grid {
app.add_systems(Startup, setup_grid); app.add_systems(Startup, setup_grid);
} else { } else {
@ -92,6 +102,14 @@ fn main() {
}); });
} }
if args.respawn {
if args.grid {
app.add_systems(Update, (despawn_ui, setup_grid).chain());
} else {
app.add_systems(Update, (despawn_ui, setup_flex).chain());
}
}
app.insert_resource(args).run(); app.insert_resource(args).run();
} }
@ -119,7 +137,6 @@ fn button_system(
} }
fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) { fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) {
warn!(include_str!("warning_string.txt"));
let image = if 0 < args.image_freq { let image = if 0 < args.image_freq {
Some(asset_server.load("branding/icon.png")) Some(asset_server.load("branding/icon.png"))
} else { } else {
@ -134,7 +151,6 @@ fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
}; };
let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8); let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8);
commands.spawn(Camera2d);
commands commands
.spawn(Node { .spawn(Node {
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
@ -171,7 +187,6 @@ fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
} }
fn setup_grid(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) { fn setup_grid(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) {
warn!(include_str!("warning_string.txt"));
let image = if 0 < args.image_freq { let image = if 0 < args.image_freq {
Some(asset_server.load("branding/icon.png")) Some(asset_server.load("branding/icon.png"))
} else { } else {
@ -186,7 +201,6 @@ fn setup_grid(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
}; };
let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8); let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8);
commands.spawn(Camera2d);
commands commands
.spawn(Node { .spawn(Node {
display: Display::Grid, display: Display::Grid,
@ -268,3 +282,7 @@ fn spawn_button(
}); });
} }
} }
fn despawn_ui(mut commands: Commands, root_node: Single<Entity, (With<Node>, Without<Parent>)>) {
commands.entity(*root_node).despawn_recursive();
}