UI Node's Style changes are ignored for unparented nodes as long as the parent exists
Bevy version
0.12.1
What you did
I made a floating panel (based on NodeBundle) that needs to be absolutely positioned to the whole window. The panel is first spawned as a child of another entity (to have access to the ChildBuilder needed to populate it's content), then I call remove_parent() on the panel (via commands) with a system in Update.
What went wrong
- Changes to the
Stylecomponent aren't reflected on the panel, i.e. left/top positioning and display properties are ignored. Except when the Window is resized.
Additional information
The Interaction component is updated as if the Style didn't change, so it seems the issue is around the calculation of the transforms or updates to the Node props.
The Style is correctly processed if the original parent node is despawned, so this works as expected:
let mut floating_panel_id = Entity::PLACEHOLDER;
commands
.spawn_empty()
.with_children(|parent| {
// The FloatingPanel has a component that tells an `Update` system to remove the parent
let (panel_id, mut container) = FloatingPanel::build(/*args*/);
floating_panel_id = panel_id;
container.with_children(|parent| {
// populate content
});
})
.despawn();
While this does not work:
let mut floating_panel_id = Entity::PLACEHOLDER;
// entity is a NodeBundle and a part of the working UI
commands.entity(entity).with_children(|parent| {
let (panel_id, mut container) = FloatingPanel::build(/*args*/);
floating_panel_id = panel_id;
container.with_children(|parent| {
// populate content
});
});
Removing the parent:
// Necessary, as you cannot remove the parent immediately after spawning due to how the hierarchy systems work
fn remove_node_parent(
q_to_remove: Query<Entity, Added<RemoveParent>>,
mut commands: Commands,
) {
for entity in &q_to_remove {
commands
.entity(entity)
.remove_parent()
.remove::<RemoveParent>();
}
}
EDIT: After more trial and error, it seems that even if the panel is spawn directly without a parent the loss of updates can occur. Randomly, I would say. I had a working version, but then it stopped working after a few builds even though I did not touch this part. The key difference between when it is working and when it's not seems to be about when the panel is spawned. During Startup always seem to work. During Update, usually not.