diff --git a/crates/bevy_render/Cargo.toml b/crates/bevy_render/Cargo.toml index a1643eafd5895..8d646a6d6f64a 100644 --- a/crates/bevy_render/Cargo.toml +++ b/crates/bevy_render/Cargo.toml @@ -34,6 +34,7 @@ bevy_core = { path = "../bevy_core", version = "0.8.0-dev" } bevy_derive = { path = "../bevy_derive", version = "0.8.0-dev" } bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev" } bevy_encase_derive = { path = "../bevy_encase_derive", version = "0.8.0-dev" } +bevy_log = { path = "../bevy_log", version = "0.8.0-dev" } bevy_math = { path = "../bevy_math", version = "0.8.0-dev" } bevy_mikktspace = { path = "../bevy_mikktspace", version = "0.8.0-dev" } bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] } diff --git a/crates/bevy_render/src/extract_resource.rs b/crates/bevy_render/src/extract_resource.rs index 57be9ab7e1c82..0e57d1d123307 100644 --- a/crates/bevy_render/src/extract_resource.rs +++ b/crates/bevy_render/src/extract_resource.rs @@ -1,7 +1,7 @@ use std::marker::PhantomData; use bevy_app::{App, Plugin}; -use bevy_ecs::system::{Commands, Res, Resource}; +use bevy_ecs::system::{Commands, Local, Res, ResMut, Resource}; pub use bevy_render_macros::ExtractResource; use crate::{Extract, RenderApp, RenderStage}; @@ -38,12 +38,26 @@ impl Plugin for ExtractResourcePlugin { } /// This system extracts the resource of the corresponding [`Resource`] type -/// by cloning it. pub fn extract_resource( mut commands: Commands, - resource: Extract>, + main_resource: Extract>, + target_resource: Option>, + #[cfg(debug_assertions)] mut has_warned_on_remove: Local, ) { - if resource.is_changed() { - commands.insert_resource(R::extract_resource(&*resource)); + if let Some(mut target_resource) = target_resource { + if main_resource.is_changed() { + *target_resource = R::extract_resource(&*main_resource); + } + } else { + #[cfg(debug_assertions)] + if !main_resource.is_added() && !*has_warned_on_remove { + *has_warned_on_remove = true; + bevy_log::warn!( + "Removing resource {} from render world not expected, adding using `Commands`. + This may decrease performance", + std::any::type_name::() + ); + } + commands.insert_resource(R::extract_resource(&*main_resource)); } }