Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_render/src/sync_component.rs
6595 views
1
use core::marker::PhantomData;
2
3
use bevy_app::{App, Plugin};
4
use bevy_ecs::component::Component;
5
6
use crate::sync_world::{EntityRecord, PendingSyncEntity, SyncToRenderWorld};
7
8
/// Plugin that registers a component for automatic sync to the render world. See [`SyncWorldPlugin`] for more information.
9
///
10
/// This plugin is automatically added by [`ExtractComponentPlugin`], and only needs to be added for manual extraction implementations.
11
///
12
/// # Implementation details
13
///
14
/// It adds [`SyncToRenderWorld`] as a required component to make the [`SyncWorldPlugin`] aware of the component, and
15
/// handles cleanup of the component in the render world when it is removed from an entity.
16
///
17
/// # Warning
18
/// When the component is removed from the main world entity, all components are removed from the entity in the render world.
19
/// This is done in order to handle components with custom extraction logic and derived state.
20
///
21
/// [`ExtractComponentPlugin`]: crate::extract_component::ExtractComponentPlugin
22
/// [`SyncWorldPlugin`]: crate::sync_world::SyncWorldPlugin
23
pub struct SyncComponentPlugin<C: Component>(PhantomData<C>);
24
25
impl<C: Component> Default for SyncComponentPlugin<C> {
26
fn default() -> Self {
27
Self(PhantomData)
28
}
29
}
30
31
impl<C: Component> Plugin for SyncComponentPlugin<C> {
32
fn build(&self, app: &mut App) {
33
app.register_required_components::<C, SyncToRenderWorld>();
34
35
app.world_mut()
36
.register_component_hooks::<C>()
37
.on_remove(|mut world, context| {
38
let mut pending = world.resource_mut::<PendingSyncEntity>();
39
pending.push(EntityRecord::ComponentRemoved(context.entity));
40
});
41
}
42
}
43
44