Path: blob/main/crates/bevy_render/src/sync_component.rs
6595 views
use core::marker::PhantomData;12use bevy_app::{App, Plugin};3use bevy_ecs::component::Component;45use crate::sync_world::{EntityRecord, PendingSyncEntity, SyncToRenderWorld};67/// Plugin that registers a component for automatic sync to the render world. See [`SyncWorldPlugin`] for more information.8///9/// This plugin is automatically added by [`ExtractComponentPlugin`], and only needs to be added for manual extraction implementations.10///11/// # Implementation details12///13/// It adds [`SyncToRenderWorld`] as a required component to make the [`SyncWorldPlugin`] aware of the component, and14/// handles cleanup of the component in the render world when it is removed from an entity.15///16/// # Warning17/// When the component is removed from the main world entity, all components are removed from the entity in the render world.18/// This is done in order to handle components with custom extraction logic and derived state.19///20/// [`ExtractComponentPlugin`]: crate::extract_component::ExtractComponentPlugin21/// [`SyncWorldPlugin`]: crate::sync_world::SyncWorldPlugin22pub struct SyncComponentPlugin<C: Component>(PhantomData<C>);2324impl<C: Component> Default for SyncComponentPlugin<C> {25fn default() -> Self {26Self(PhantomData)27}28}2930impl<C: Component> Plugin for SyncComponentPlugin<C> {31fn build(&self, app: &mut App) {32app.register_required_components::<C, SyncToRenderWorld>();3334app.world_mut()35.register_component_hooks::<C>()36.on_remove(|mut world, context| {37let mut pending = world.resource_mut::<PendingSyncEntity>();38pending.push(EntityRecord::ComponentRemoved(context.entity));39});40}41}424344