diff --git a/Cargo.toml b/Cargo.toml index 2b09032015..978c1b754f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1604,6 +1604,17 @@ description = "Shows how to create a custom diagnostic" category = "Diagnostics" wasm = true +[[example]] +name = "enabling_disabling_diagnostic" +path = "examples/diagnostics/enabling_disabling_diagnostic.rs" +doc-scrape-examples = true + +[package.metadata.example.enabling_disabling_diagnostic] +name = "Enabling/disabling diagnostic" +description = "Shows how to disable/re-enable a Diagnostic during runtime" +category = "Diagnostics" +wasm = true + # ECS (Entity Component System) [[example]] name = "ecs_guide" diff --git a/examples/README.md b/examples/README.md index 85b56b4d8e..b1fa8ac61d 100644 --- a/examples/README.md +++ b/examples/README.md @@ -265,6 +265,7 @@ Example | Description Example | Description --- | --- [Custom Diagnostic](../examples/diagnostics/custom_diagnostic.rs) | Shows how to create a custom diagnostic +[Enabling/disabling diagnostic](../examples/diagnostics/enabling_disabling_diagnostic.rs) | Shows how to disable/re-enable a Diagnostic during runtime [Log Diagnostics](../examples/diagnostics/log_diagnostics.rs) | Add a plugin that logs diagnostics, like frames per second (FPS), to the console ## ECS (Entity Component System) diff --git a/examples/diagnostics/enabling_disabling_diagnostic.rs b/examples/diagnostics/enabling_disabling_diagnostic.rs new file mode 100644 index 0000000000..27562bb98c --- /dev/null +++ b/examples/diagnostics/enabling_disabling_diagnostic.rs @@ -0,0 +1,30 @@ +//! Shows how to disable/re-enable a Diagnostic during runtime + +use std::time::Duration; + +use bevy::{ + diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, + prelude::*, + time::common_conditions::on_timer, +}; + +fn main() { + App::new() + .add_plugins(( + DefaultPlugins, + FrameTimeDiagnosticsPlugin, + LogDiagnosticsPlugin::default(), + )) + .add_systems( + Update, + toggle.run_if(on_timer(Duration::from_secs_f32(10.0))), + ) + .run(); +} + +fn toggle(mut store: ResMut) { + for diag in store.iter_mut() { + info!("toggling diagnostic {}", diag.path()); + diag.is_enabled = !diag.is_enabled; + } +}