diff --git a/crates/bevy_asset/Cargo.toml b/crates/bevy_asset/Cargo.toml index 31b5d9f0759357..29535dfaf4e254 100644 --- a/crates/bevy_asset/Cargo.toml +++ b/crates/bevy_asset/Cargo.toml @@ -19,6 +19,7 @@ filesystem_watcher = ["notify"] [dependencies] # bevy bevy_app = { path = "../bevy_app", version = "0.3.0" } +bevy_diagnostic = { path = "../bevy_diagnostic", version = "0.3.0" } bevy_ecs = { path = "../bevy_ecs", version = "0.3.0" } bevy_reflect = { path = "../bevy_reflect", version = "0.3.0", features = ["bevy"] } bevy_tasks = { path = "../bevy_tasks", version = "0.3.0" } diff --git a/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs b/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs new file mode 100644 index 00000000000000..db3d773d5004db --- /dev/null +++ b/crates/bevy_asset/src/diagnostic/asset_count_diagnostics_plugin.rs @@ -0,0 +1,35 @@ +use crate::{Asset, Assets}; +use bevy_app::prelude::*; +use bevy_diagnostic::{Diagnostic, DiagnosticId, Diagnostics}; +use bevy_ecs::{IntoSystem, Res, ResMut}; + +/// Adds "asset count" diagnostic to an App +#[derive(Default)] +pub struct AssetCountDiagnosticsPlugin { + marker: std::marker::PhantomData, +} + +impl Plugin for AssetCountDiagnosticsPlugin { + fn build(&self, app: &mut AppBuilder) { + app.add_startup_system(Self::setup_system.system()) + .add_system(Self::diagnostic_system.system()); + } +} + +impl AssetCountDiagnosticsPlugin { + pub fn diagnostic_id() -> DiagnosticId { + DiagnosticId(T::TYPE_UUID) + } + + pub fn setup_system(mut diagnostics: ResMut) { + diagnostics.add(Diagnostic::new( + Self::diagnostic_id(), + &format!("asset_count {}", std::any::type_name::()), + 20, + )); + } + + pub fn diagnostic_system(mut diagnostics: ResMut, assets: Res>) { + diagnostics.add_measurement(Self::diagnostic_id(), assets.len() as f64); + } +} diff --git a/crates/bevy_asset/src/diagnostic/mod.rs b/crates/bevy_asset/src/diagnostic/mod.rs new file mode 100644 index 00000000000000..f093527f9755ab --- /dev/null +++ b/crates/bevy_asset/src/diagnostic/mod.rs @@ -0,0 +1,2 @@ +mod asset_count_diagnostics_plugin; +pub use asset_count_diagnostics_plugin::AssetCountDiagnosticsPlugin; diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index 1e77e144436260..e527b79381a78b 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -1,5 +1,6 @@ mod asset_server; mod assets; +pub mod diagnostic; #[cfg(all( feature = "filesystem_watcher", all(not(target_arch = "wasm32"), not(target_os = "android")) diff --git a/examples/diagnostics/log_diagnostics.rs b/examples/diagnostics/log_diagnostics.rs index 2a9dca9e94643c..a48efde05623fc 100644 --- a/examples/diagnostics/log_diagnostics.rs +++ b/examples/diagnostics/log_diagnostics.rs @@ -15,5 +15,7 @@ fn main() { // .add_plugin(bevy::wgpu::diagnostic::WgpuResourceDiagnosticsPlugin::default()) // Uncomment this to add an entity count diagnostics: // .add_plugin(bevy::diagnostic::EntityCountDiagnosticsPlugin::default()) + // Uncomment this to add an asset count diagnostics: + // .add_plugin(bevy::asset::diagnostic::AssetCountDiagnosticsPlugin::::default()) .run(); }