From 03ee959809eed3d7a94cfda6212fd53ebc2428d3 Mon Sep 17 00:00:00 2001 From: Doonv <58695417+doonv@users.noreply.github.com> Date: Thu, 18 Jan 2024 22:33:22 +0200 Subject: [PATCH] Fix panic on Text UI without Cameras (#11405) # Objective Fix https://github.com/bevyengine/bevy/issues/11396. ## Solution Don't panic on taffy node not existing. Plus minor warning text improvement. --- crates/bevy_ui/src/layout/mod.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ui/src/layout/mod.rs b/crates/bevy_ui/src/layout/mod.rs index e377d882d8..25df73d6af 100644 --- a/crates/bevy_ui/src/layout/mod.rs +++ b/crates/bevy_ui/src/layout/mod.rs @@ -102,10 +102,15 @@ impl UiSurface { } } - /// Update the `MeasureFunc` of the taffy node corresponding to the given [`Entity`]. - pub fn update_measure(&mut self, entity: Entity, measure_func: taffy::node::MeasureFunc) { - let taffy_node = self.entity_to_taffy.get(&entity).unwrap(); - self.taffy.set_measure(*taffy_node, Some(measure_func)).ok(); + /// Update the `MeasureFunc` of the taffy node corresponding to the given [`Entity`] if the node exists. + pub fn try_update_measure( + &mut self, + entity: Entity, + measure_func: taffy::node::MeasureFunc, + ) -> Option<()> { + let taffy_node = self.entity_to_taffy.get(&entity)?; + + self.taffy.set_measure(*taffy_node, Some(measure_func)).ok() } /// Update the children of the taffy node corresponding to the given [`Entity`]. @@ -305,7 +310,7 @@ pub fn ui_layout_system( Some(camera_entity) => { let Ok((_, camera)) = cameras.get(camera_entity) else { warn!( - "TargetCamera is pointing to a camera {:?} which doesn't exist", + "TargetCamera (of root UI node {entity:?}) is pointing to a camera {:?} which doesn't exist", camera_entity ); continue; @@ -356,7 +361,7 @@ pub fn ui_layout_system( } for (entity, mut content_size) in &mut measure_query { if let Some(measure_func) = content_size.measure_func.take() { - ui_surface.update_measure(entity, measure_func); + ui_surface.try_update_measure(entity, measure_func); } }