Skip to content

Commit 2d91a6f

Browse files
authored
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```
1 parent 4eaebd4 commit 2d91a6f

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

examples/stress_tests/many_buttons.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ struct Args {
4242
/// use the grid layout model
4343
#[argh(switch)]
4444
grid: bool,
45+
46+
/// at the start of each frame despawn any existing UI nodes and spawn a new UI tree
47+
#[argh(switch)]
48+
respawn: bool,
4549
}
4650

4751
/// This example shows what happens when there is a lot of buttons on screen.
@@ -52,6 +56,8 @@ fn main() {
5256
#[cfg(target_arch = "wasm32")]
5357
let args = Args::from_args(&[], &[]).unwrap();
5458

59+
warn!(include_str!("warning_string.txt"));
60+
5561
let mut app = App::new();
5662

5763
app.add_plugins((
@@ -72,6 +78,10 @@ fn main() {
7278
})
7379
.add_systems(Update, (button_system, set_text_colors_changed));
7480

81+
app.add_systems(Startup, |mut commands: Commands| {
82+
commands.spawn(Camera2d);
83+
});
84+
7585
if args.grid {
7686
app.add_systems(Startup, setup_grid);
7787
} else {
@@ -92,6 +102,14 @@ fn main() {
92102
});
93103
}
94104

105+
if args.respawn {
106+
if args.grid {
107+
app.add_systems(Update, (despawn_ui, setup_grid).chain());
108+
} else {
109+
app.add_systems(Update, (despawn_ui, setup_flex).chain());
110+
}
111+
}
112+
95113
app.insert_resource(args).run();
96114
}
97115

@@ -119,7 +137,6 @@ fn button_system(
119137
}
120138

121139
fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) {
122-
warn!(include_str!("warning_string.txt"));
123140
let image = if 0 < args.image_freq {
124141
Some(asset_server.load("branding/icon.png"))
125142
} else {
@@ -134,7 +151,6 @@ fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
134151
};
135152

136153
let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8);
137-
commands.spawn(Camera2d);
138154
commands
139155
.spawn(Node {
140156
flex_direction: FlexDirection::Column,
@@ -171,7 +187,6 @@ fn setup_flex(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
171187
}
172188

173189
fn setup_grid(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<Args>) {
174-
warn!(include_str!("warning_string.txt"));
175190
let image = if 0 < args.image_freq {
176191
Some(asset_server.load("branding/icon.png"))
177192
} else {
@@ -186,7 +201,6 @@ fn setup_grid(mut commands: Commands, asset_server: Res<AssetServer>, args: Res<
186201
};
187202

188203
let as_rainbow = |i: usize| Color::hsl((i as f32 / buttons_f) * 360.0, 0.9, 0.8);
189-
commands.spawn(Camera2d);
190204
commands
191205
.spawn(Node {
192206
display: Display::Grid,
@@ -268,3 +282,7 @@ fn spawn_button(
268282
});
269283
}
270284
}
285+
286+
fn despawn_ui(mut commands: Commands, root_node: Single<Entity, (With<Node>, Without<Parent>)>) {
287+
commands.entity(*root_node).despawn_recursive();
288+
}

0 commit comments

Comments
 (0)