|
1 | 1 | use std::marker::PhantomData;
|
2 | 2 |
|
3 | 3 | use bevy_app::{App, Plugin};
|
4 |
| -use bevy_ecs::system::{Commands, Res, Resource}; |
| 4 | +use bevy_ecs::system::{Commands, Local, Res, ResMut, Resource}; |
5 | 5 | pub use bevy_render_macros::ExtractResource;
|
6 | 6 |
|
7 | 7 | use crate::{Extract, RenderApp, RenderStage};
|
@@ -38,12 +38,26 @@ impl<R: ExtractResource> Plugin for ExtractResourcePlugin<R> {
|
38 | 38 | }
|
39 | 39 |
|
40 | 40 | /// This system extracts the resource of the corresponding [`Resource`] type
|
41 |
| -/// by cloning it. |
42 | 41 | pub fn extract_resource<R: ExtractResource>(
|
43 | 42 | mut commands: Commands,
|
44 |
| - resource: Extract<Res<R::Source>>, |
| 43 | + main_resource: Extract<Res<R::Source>>, |
| 44 | + target_resource: Option<ResMut<R>>, |
| 45 | + #[cfg(debug_assertions)] mut has_warned_on_remove: Local<bool>, |
45 | 46 | ) {
|
46 |
| - if resource.is_changed() { |
47 |
| - commands.insert_resource(R::extract_resource(&*resource)); |
| 47 | + if let Some(mut target_resource) = target_resource { |
| 48 | + if main_resource.is_changed() { |
| 49 | + *target_resource = R::extract_resource(&*main_resource); |
| 50 | + } |
| 51 | + } else { |
| 52 | + #[cfg(debug_assertions)] |
| 53 | + if !main_resource.is_added() && !*has_warned_on_remove { |
| 54 | + *has_warned_on_remove = true; |
| 55 | + bevy_log::warn!( |
| 56 | + "Removing resource {} from render world not expected, adding using `Commands`. |
| 57 | + This may decrease performance", |
| 58 | + std::any::type_name::<R>() |
| 59 | + ); |
| 60 | + } |
| 61 | + commands.insert_resource(R::extract_resource(&*main_resource)); |
48 | 62 | }
|
49 | 63 | }
|
0 commit comments