During bevy setup, I've spawned multiple rectangles with two different colors. How do I update these rectangles with a new color material?
// in setup
commands.spawn(
(MaterialMesh2dBundle {
mesh: Mesh2dHandle(meshes.add(Rectangle::new(10., 10.))),
material: if dull_world.is_live(row_index, col_index) {
materials.add(Color::PURPLE)
} else {
materials.add(Color::GREEN)
},
transform: Transform::from_xyz(
(row_index as f32 - row_len as f32 / 2.) * 10.,
(col_index as f32 - col_len as f32 / 2.) * 10.,
0.0,
),
..default()
}),
);
One way to accomplish this is to reuse the material handles you're creating by inserting them as Resources.
Then during your update call, query the entities that match
Query<&mut Handle<ColorMaterial>>with the resource you wantRes<CellDeadColor>. Since the handles exist already, you simply need to clone them and apply them to the color material handler.