diff --git a/Cargo.toml b/Cargo.toml index 76a70f715c..013eb68681 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,6 +109,14 @@ path = "examples/app/headless.rs" name = "plugin" path = "examples/app/plugin.rs" +[[example]] +name = "custom_diagnostic" +path = "examples/diagnostics/custom_diagnostic.rs" + +[[example]] +name = "print_diagnostics" +path = "examples/diagnostics/print_diagnostics.rs" + [[example]] name = "event" path = "examples/ecs/event.rs" diff --git a/crates/bevy_diagnostic/src/print_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/print_diagnostics_plugin.rs index 88abaa8176..4fed9fb53e 100644 --- a/crates/bevy_diagnostic/src/print_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/print_diagnostics_plugin.rs @@ -73,7 +73,7 @@ impl PrintDiagnosticsPlugin { if state.elapsed >= state.wait_seconds { state.elapsed = 0.0; println!("Diagnostics:"); - println!("{}", "-".repeat(50)); + println!("{}", "-".repeat(60)); if let Some(ref filter) = state.filter { for diagnostic in filter.iter().map(|id| diagnostics.get(*id).unwrap()) { Self::print_diagnostic(diagnostic); @@ -95,7 +95,7 @@ impl PrintDiagnosticsPlugin { if state.elapsed >= state.wait_seconds { state.elapsed = 0.0; println!("Diagnostics (Debug):"); - println!("{}", "-".repeat(30)); + println!("{}", "-".repeat(60)); if let Some(ref filter) = state.filter { for diagnostic in filter.iter().map(|id| diagnostics.get(*id).unwrap()) { println!("{:#?}\n", diagnostic); diff --git a/examples/diagnostics/custom_diagnostic.rs b/examples/diagnostics/custom_diagnostic.rs new file mode 100644 index 0000000000..e17f525e9a --- /dev/null +++ b/examples/diagnostics/custom_diagnostic.rs @@ -0,0 +1,33 @@ +use bevy::{ + diagnostic::{Diagnostic, DiagnosticId, Diagnostics, PrintDiagnosticsPlugin}, + prelude::*, +}; + +fn main() { + App::build() + .add_default_plugins() + // The "print diagnostics" plugin is optional. It just visualizes our diagnostics in the console + .add_plugin(PrintDiagnosticsPlugin::default()) + .add_startup_system(setup_diagnostic_system.system()) + .add_system(my_system.system()) + .run(); +} + +// All diagnostics should have a unique DiagnosticId. for each new diagnostic, generate a new random number +pub const SYSTEM_ITERATION_COUNT: DiagnosticId = + DiagnosticId::from_u128(337040787172757619024841343456040760896); + +fn setup_diagnostic_system(mut diagnostics: ResourceMut) { + // Diagnostics must be initialized before measurements can be added. + // In general it's a good idea to set them up in a "startup system". + diagnostics.add(Diagnostic::new( + SYSTEM_ITERATION_COUNT, + "system_iteration_count", + 10, + )); +} + +fn my_system(mut diagnostics: ResourceMut) { + // Add a measurement of 10.0 for our diagnostic each time this system runs + diagnostics.add_measurement(SYSTEM_ITERATION_COUNT, 10.0); +} diff --git a/examples/diagnostics/print_diagnostics.rs b/examples/diagnostics/print_diagnostics.rs new file mode 100644 index 0000000000..e0d18155fd --- /dev/null +++ b/examples/diagnostics/print_diagnostics.rs @@ -0,0 +1,12 @@ +use bevy::{ + diagnostic::{FrameTimeDiagnosticsPlugin, PrintDiagnosticsPlugin}, + prelude::*, +}; + +fn main() { + App::build() + .add_default_plugins() + .add_plugin(FrameTimeDiagnosticsPlugin::default()) + .add_plugin(PrintDiagnosticsPlugin::default()) + .run(); +}