no-camera many_buttons argument, only emit UI camera warnings once (#17557)

# Objective

* Add a `no-camera` argument to the `many_buttons` stress test example.
* Only emit the UI "no camera found" warnings once.
This commit is contained in:
ickshonpe 2025-01-28 18:04:52 +00:00 committed by GitHub
parent b039bf6768
commit a80263a5bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 8 deletions

View File

@ -12,6 +12,7 @@ use bevy_math::{UVec2, Vec2};
use bevy_render::camera::{Camera, NormalizedRenderTarget};
use bevy_sprite::BorderRect;
use bevy_transform::components::Transform;
use bevy_utils::once;
use bevy_window::{PrimaryWindow, Window, WindowScaleFactorChanged};
use thiserror::Error;
use tracing::warn;
@ -164,10 +165,10 @@ pub fn ui_layout_system(
match camera_with_default(target_camera) {
Some(camera_entity) => {
let Ok((_, camera)) = cameras.get(camera_entity) else {
warn!(
once!(warn!(
"UiTargetCamera (of root UI node {entity}) is pointing to a camera {} which doesn't exist",
camera_entity
);
));
return;
};
let layout_info = camera_layout_info
@ -177,13 +178,13 @@ pub fn ui_layout_system(
}
None => {
if cameras.is_empty() {
warn!("No camera found to render UI to. To fix this, add at least one camera to the scene.");
once!(warn!("No camera found to render UI to. To fix this, add at least one camera to the scene."));
} else {
warn!(
once!(warn!(
"Multiple cameras found, causing UI target ambiguity. \
To fix this, add an explicit `UiTargetCamera` component to the root UI node {}",
entity
);
));
}
}
}

View File

@ -50,6 +50,10 @@ struct Args {
/// set the root node to display none, removing all nodes from the layout.
#[argh(switch)]
display_none: bool,
/// spawn the layout without a camera
#[argh(switch)]
no_camera: bool,
}
/// This example shows what happens when there is a lot of buttons on screen.
@ -82,9 +86,11 @@ fn main() {
})
.add_systems(Update, (button_system, set_text_colors_changed));
app.add_systems(Startup, |mut commands: Commands| {
commands.spawn(Camera2d);
});
if !args.no_camera {
app.add_systems(Startup, |mut commands: Commands| {
commands.spawn(Camera2d);
});
}
if args.grid {
app.add_systems(Startup, setup_grid);