From a80263a5bface76bb27be97091ba4ddb51c5c929 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Tue, 28 Jan 2025 18:04:52 +0000 Subject: [PATCH] `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. --- crates/bevy_ui/src/layout/mod.rs | 11 ++++++----- examples/stress_tests/many_buttons.rs | 12 +++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/crates/bevy_ui/src/layout/mod.rs b/crates/bevy_ui/src/layout/mod.rs index 4eea61a48a..7288fbec1b 100644 --- a/crates/bevy_ui/src/layout/mod.rs +++ b/crates/bevy_ui/src/layout/mod.rs @@ -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 - ); + )); } } } diff --git a/examples/stress_tests/many_buttons.rs b/examples/stress_tests/many_buttons.rs index b561634254..250ea79977 100644 --- a/examples/stress_tests/many_buttons.rs +++ b/examples/stress_tests/many_buttons.rs @@ -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);