From a9b0b4e7f7a51db77df0ca75c7029d7b60daa369 Mon Sep 17 00:00:00 2001 From: charlotte Date: Fri, 11 Apr 2025 16:18:26 -0700 Subject: [PATCH] Mark render assets as modified when removed from the asset server (#18814) # Objective Fixes #18808 ## Solution When an asset emits a removed event, mark it as modified in the render world to ensure any appropriate bookkeeping runs as necessary. --- crates/bevy_render/src/render_asset.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/crates/bevy_render/src/render_asset.rs b/crates/bevy_render/src/render_asset.rs index 6626cb7797..0568b40391 100644 --- a/crates/bevy_render/src/render_asset.rs +++ b/crates/bevy_render/src/render_asset.rs @@ -246,10 +246,6 @@ pub(crate) fn extract_render_asset( let mut modified = >::default(); for event in events.read() { - #[expect( - clippy::match_same_arms, - reason = "LoadedWithDependencies is marked as a TODO, so it's likely this will no longer lint soon." - )] match event { AssetEvent::Added { id } => { needs_extracting.insert(*id); @@ -258,9 +254,20 @@ pub(crate) fn extract_render_asset( needs_extracting.insert(*id); modified.insert(*id); } - AssetEvent::Removed { .. } => { - // We don't care that the asset was removed from Assets in the main world. - // An asset is only removed from RenderAssets when its last handle is dropped (AssetEvent::Unused). + AssetEvent::Removed { id, .. } => { + // Normally, we consider an asset removed from the render world only + // when it's final handle is dropped triggering an `AssetEvent::Unused` + // event. However, removal without unused can happen when the asset + // is explicitly removed from the asset server and re-added by the user. + // We mark the asset as modified in this case to ensure that + // any necessary render world bookkeeping still runs. + + // TODO: consider removing this check and just emitting Unused after + // Removed to ensure that the asset is always "really" removed from the + // render world when the last strong handle is dropped. + if !removed.contains(id) { + modified.insert(*id); + } } AssetEvent::Unused { id } => { needs_extracting.remove(id);