Skip to content
22 changes: 8 additions & 14 deletions examples/2d/sprite_scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,25 +113,22 @@ fn setup_sprites(mut commands: Commands, asset_server: Res<AssetServer>) {
];

for rect in rects {
let mut cmd = commands.spawn((
commands.spawn((
Sprite {
image: rect.texture,
custom_size: Some(rect.size),
image_mode: rect.image_mode,
..default()
},
rect.transform,
));

cmd.with_children(|builder| {
builder.spawn((
children![(
Text2d::new(rect.text),
TextLayout::new_with_justify(Justify::Center),
TextFont::from_font_size(15.),
Transform::from_xyz(0., -0.5 * rect.size.y - 10., 0.),
bevy::sprite::Anchor::TOP_CENTER,
));
});
)],
));
}
}

Expand Down Expand Up @@ -257,7 +254,7 @@ fn setup_texture_atlas(
];

for sprite_sheet in sprite_sheets {
let mut cmd = commands.spawn((
commands.spawn((
Sprite {
image_mode: sprite_sheet.image_mode,
custom_size: Some(sprite_sheet.size),
Expand All @@ -266,17 +263,14 @@ fn setup_texture_atlas(
sprite_sheet.indices,
sprite_sheet.timer,
sprite_sheet.transform,
));

cmd.with_children(|builder| {
builder.spawn((
children![(
Text2d::new(sprite_sheet.text),
TextLayout::new_with_justify(Justify::Center),
TextFont::from_font_size(15.),
Transform::from_xyz(0., -0.5 * sprite_sheet.size.y - 10., 0.),
bevy::sprite::Anchor::TOP_CENTER,
));
});
)],
));
}
}

Expand Down
70 changes: 36 additions & 34 deletions examples/2d/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,44 +132,46 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
Text2dShadow::default(),
));

commands
.spawn((
Sprite {
color: Color::Srgba(LIGHT_CYAN),
custom_size: Some(Vec2::new(10., 10.)),
..Default::default()
},
Transform::from_translation(250. * Vec3::Y),
))
.with_children(|commands| {
for (text_anchor, color) in [
commands.spawn((
Sprite {
color: Color::Srgba(LIGHT_CYAN),
custom_size: Some(Vec2::new(10., 10.)),
..Default::default()
},
Transform::from_translation(250. * Vec3::Y),
Children::spawn(SpawnIter(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a fan of this one, but won't block on it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I am either but I also didn't know about this functionality so maybe it's worth it!

Copy link
Contributor Author

@janis-bhm janis-bhm Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the ecs/hierarchy.rs example would lend itself quite well to demonstrating the different possible methods for inserting children/handling relations. Would you be more against having this in this example if that example included a demonstration of SpawnIter?

Copy link
Member

@janhohenheim janhohenheim Sep 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API in this case just looks clunky to me, no matter if it's also used in other places. But that might just be me being more used to the "old" way :)

[
(Anchor::TOP_LEFT, Color::Srgba(LIGHT_SALMON)),
(Anchor::TOP_RIGHT, Color::Srgba(LIGHT_GREEN)),
(Anchor::BOTTOM_RIGHT, Color::Srgba(LIGHT_BLUE)),
(Anchor::BOTTOM_LEFT, Color::Srgba(LIGHT_YELLOW)),
] {
commands
.spawn((
Text2d::new(" Anchor".to_string()),
slightly_smaller_text_font.clone(),
text_anchor,
TextBackgroundColor(Color::WHITE.darker(0.8)),
Transform::from_translation(-1. * Vec3::Z),
))
.with_child((
TextSpan("::".to_string()),
slightly_smaller_text_font.clone(),
TextColor(LIGHT_GREY.into()),
TextBackgroundColor(DARK_BLUE.into()),
))
.with_child((
TextSpan(format!("{text_anchor:?} ")),
slightly_smaller_text_font.clone(),
TextColor(color),
TextBackgroundColor(color.darker(0.3)),
));
}
});
]
.into_iter()
.map(move |(text_anchor, color)| {
(
Text2d::new(" Anchor".to_string()),
slightly_smaller_text_font.clone(),
text_anchor,
TextBackgroundColor(Color::WHITE.darker(0.8)),
Transform::from_translation(-1. * Vec3::Z),
children![
(
TextSpan("::".to_string()),
slightly_smaller_text_font.clone(),
TextColor(LIGHT_GREY.into()),
TextBackgroundColor(DARK_BLUE.into()),
),
(
TextSpan(format!("{text_anchor:?} ")),
slightly_smaller_text_font.clone(),
TextColor(color),
TextBackgroundColor(color.darker(0.3)),
)
],
)
}),
)),
));
}

fn animate_translation(
Expand Down
46 changes: 20 additions & 26 deletions examples/animation/animated_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,35 +151,29 @@ fn setup(
player,
))
.id();
commands
.entity(planet_entity)
.insert(AnimationTarget {
commands.entity(planet_entity).insert((
AnimationTarget {
id: planet_animation_target_id,
player: planet_entity,
})
.with_children(|p| {
// This entity is just used for animation, but doesn't display anything
p.spawn((
Transform::default(),
Visibility::default(),
orbit_controller,
},
Children::spawn_one((
Transform::default(),
Visibility::default(),
orbit_controller,
AnimationTarget {
id: orbit_controller_animation_target_id,
player: planet_entity,
},
Children::spawn_one((
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.9, 0.3))),
Transform::from_xyz(1.5, 0.0, 0.0),
AnimationTarget {
id: orbit_controller_animation_target_id,
id: satellite_animation_target_id,
player: planet_entity,
},
))
.with_children(|p| {
// The satellite, placed at a distance of the planet
p.spawn((
Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.9, 0.3))),
Transform::from_xyz(1.5, 0.0, 0.0),
AnimationTarget {
id: satellite_animation_target_id,
player: planet_entity,
},
satellite,
));
});
});
satellite,
)),
)),
));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What stops these from being children!?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing, really, I just didn't see the point of using the macro here when it's just a single child. I'm not sure I see why using children! everywhere, even when it results in more indirection (which might get compiled out), is better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have a vague memory of someone saying that children! should always take precedence over Children::spawn or Children::spawn_one

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think using children! everywhere is preferable in learning material to lower the complexity for beginners.

}
69 changes: 32 additions & 37 deletions examples/animation/animated_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,43 +123,38 @@ fn setup(
// Build the UI. We have a parent node that covers the whole screen and
// contains the `AnimationPlayer`, as well as a child node that contains the
// text to be animated.
commands
.spawn((
// Cover the whole screen, and center contents.
Node {
position_type: PositionType::Absolute,
top: px(0),
left: px(0),
right: px(0),
bottom: px(0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
animation_player,
AnimationGraphHandle(animation_graph),
))
.with_children(|builder| {
// Build the text node.
let player = builder.target_entity();
builder
.spawn((
Text::new("Bevy"),
TextFont {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 24.0,
..default()
},
TextColor(Color::Srgba(Srgba::RED)),
TextLayout::new_with_justify(Justify::Center),
))
// Mark as an animation target.
.insert(AnimationTarget {
id: animation_target_id,
player,
})
.insert(animation_target_name);
});
let mut entity = commands.spawn((
// Cover the whole screen, and center contents.
Node {
position_type: PositionType::Absolute,
top: px(0),
left: px(0),
right: px(0),
bottom: px(0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
animation_player,
AnimationGraphHandle(animation_graph),
));

let player = entity.id();
entity.insert(Children::spawn_one((
Text::new("Bevy"),
TextFont {
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
font_size: 24.0,
..default()
},
TextColor(Color::Srgba(Srgba::RED)),
TextLayout::new_with_justify(Justify::Center),
AnimationTarget {
id: animation_target_id,
player,
},
animation_target_name,
)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this one be children!?

}

// A type that represents the color of the first text section.
Expand Down
Loading
Loading