Swapping out a ColorMaterial with another existing ColorMaterial from a small set? #7497
-
Hello! I feel like this should be fairly simple, but I seem to be struggling with it. My Bevy application is going to have just a few So since I've only got a few struct Palette {
start: ColorMaterial,
end: ColorMaterial,
}
impl Default for Palette {
fn default() -> Palette {
Palette {
start: ColorMaterial::from(Color::BLUE),
end: ColorMaterial::from(Color::RED),
}
}
}
// ...
App::new()
// ...
.init_resource::<Palette>()
// ... Then creating a mesh with the commands.spawn(
(
MaterialMesh2dBundle {
mesh: meshes.add(shape::Quad::new(Vec2 { x: 20., y: 30. }).into()).into(),
material: palette.start,
transform: Transform::from_translation(Vec3::new(0., 0., 0.)),
..default()
},
Thingy
)
); Then, when I want to swap out a fn color_swap_system(
mut query: Query<(&Thingy, With<Handle<ColorMaterial>>)>
) {
for thingy in &mut query.iter() {
thingy = Palette.end
}
} However, this doesn't work because the mesh expects a All this said and not knowing much about Bevy, I don't know if storing my small set of color materials as a separate resource seems like a reasonable approach anyway, or if there are better options. Any thoughts or suggestions on how I should approach this? Thank you much! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Rather than Default, implement FromWorld. |
Beta Was this translation helpful? Give feedback.
Rather than Default, implement FromWorld.
init_resource
always uses theFromWorld
implementation — that trait just gets automatically implemented on anything that hasDefault
.