diff --git a/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs b/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs index 6c82b3ff66f1ed..0fc7575cb80f40 100644 --- a/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs +++ b/crates/bevy_hierarchy/src/valid_parent_check_plugin.rs @@ -2,12 +2,32 @@ use std::marker::PhantomData; use bevy_app::{App, CoreStage, Plugin}; use bevy_core::Name; -use bevy_ecs::prelude::*; +use bevy_ecs::{prelude::*, schedule::ShouldRun}; use bevy_log::warn; use bevy_utils::{get_short_name, HashSet}; use crate::Parent; +/// When enabled, runs [`check_hierarchy_component_has_valid_parent`]. +/// +/// This resource is added by [`ValidParentCheckPlugin`]. +/// It is enabled on debug builds and disabled in release builds by default, +/// you can update this resource at runtime to change the default behavior. +#[derive(Resource)] +pub struct ReportHierarchyIssue { + /// Whether to run [`check_hierarchy_component_has_valid_parent`]. + pub enabled: bool, + _comp: PhantomData, +} +impl Default for ReportHierarchyIssue { + fn default() -> Self { + Self { + enabled: cfg!(debug_assertions), + _comp: PhantomData, + } + } +} + /// System to print a warning for each `Entity` with a `T` component /// which parent hasn't a `T` component. /// @@ -38,6 +58,14 @@ pub fn check_hierarchy_component_has_valid_parent( } } +/// Run criteria that only allows running when [`ReportHierarchyIssue`] is enabled. +pub fn on_hierarchy_reports_enabled(report: Res>) -> ShouldRun +where + T: Component, +{ + report.enabled.into() +} + /// Print a warning for each `Entity` with a `T` component /// which parent hasn't a `T` component. /// @@ -53,7 +81,8 @@ impl Plugin for ValidParentCheckPlugin { fn build(&self, app: &mut App) { app.add_system_to_stage( CoreStage::Last, - check_hierarchy_component_has_valid_parent::, + check_hierarchy_component_has_valid_parent:: + .with_run_criteria(on_hierarchy_reports_enabled::), ); } } diff --git a/crates/bevy_internal/src/default_plugins.rs b/crates/bevy_internal/src/default_plugins.rs index 4304b4e7fa0669..07383f5023f5a1 100644 --- a/crates/bevy_internal/src/default_plugins.rs +++ b/crates/bevy_internal/src/default_plugins.rs @@ -35,11 +35,6 @@ impl PluginGroup for DefaultPlugins { group.add(bevy_input::InputPlugin::default()); group.add(bevy_window::WindowPlugin::default()); - #[cfg(debug_assertions)] - group.add(bevy_hierarchy::ValidParentCheckPlugin::< - crate::prelude::GlobalTransform, - >::default()); - #[cfg(feature = "bevy_asset")] group.add(bevy_asset::AssetPlugin::default()); @@ -55,11 +50,6 @@ impl PluginGroup for DefaultPlugins { #[cfg(feature = "bevy_render")] group.add(bevy_render::RenderPlugin::default()); - #[cfg(all(feature = "bevy_render", debug_assertions))] - group.add(bevy_hierarchy::ValidParentCheckPlugin::< - crate::prelude::ComputedVisibility, - >::default()); - #[cfg(feature = "bevy_core_pipeline")] group.add(bevy_core_pipeline::CorePipelinePlugin::default()); diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index fbe5f14923970a..10b6b0b6024e19 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -18,6 +18,7 @@ mod spatial_bundle; pub mod texture; pub mod view; +use bevy_hierarchy::ValidParentCheckPlugin; pub use extract_param::Extract; pub mod prelude { @@ -34,6 +35,7 @@ pub mod prelude { } pub use once_cell; +use prelude::ComputedVisibility; use crate::{ camera::CameraPlugin, @@ -315,7 +317,8 @@ impl Plugin for RenderPlugin { }); } - app.add_plugin(WindowRenderPlugin) + app.add_plugin(ValidParentCheckPlugin::::default()) + .add_plugin(WindowRenderPlugin) .add_plugin(CameraPlugin) .add_plugin(ViewPlugin) .add_plugin(MeshPlugin) diff --git a/crates/bevy_transform/src/lib.rs b/crates/bevy_transform/src/lib.rs index 03484997e34e36..4f391fd370bbce 100644 --- a/crates/bevy_transform/src/lib.rs +++ b/crates/bevy_transform/src/lib.rs @@ -14,6 +14,7 @@ pub mod prelude { use bevy_app::prelude::*; use bevy_ecs::prelude::*; +use bevy_hierarchy::ValidParentCheckPlugin; use prelude::{GlobalTransform, Transform}; /// A [`Bundle`] of the [`Transform`] and [`GlobalTransform`] @@ -91,6 +92,7 @@ impl Plugin for TransformPlugin { fn build(&self, app: &mut App) { app.register_type::() .register_type::() + .add_plugin(ValidParentCheckPlugin::::default()) // add transform systems to startup so the first update is "correct" .add_startup_system_to_stage( StartupStage::PostStartup,