2

我有一个查询系统,可以找到鼠标所在的对象。这不是一个按钮,但是,我想改变颜色。我不知道从哪里开始。我将查询什么属性以及如何更改它?目前,我有以下内容:

fn mouse_move(mut commands: Commands, cursor: Res<Cursor>, mut query: Query<(&Translation,&mut Sprite,&Box,&Name)>) 
{
    for (translation,mut sprite,_box,name) in &mut query.iter() {
        let cursor_tup = translate_cursor ((cursor.0,cursor.1));
        let cursor_vec = Vec3::new(cursor_tup.0,cursor_tup.1,0.0);
        if collides(cursor_vec,Vec2::new(1.0,1.0),translation.0,sprite.size) {
            println!("{}",name.0);
        }
    }
}
4

1 回答 1

4
fn mouse_move(mut commands: Commands, cursor: Res<Cursor>, mut materials: ResMut<Assets<ColorMaterial>>, mut query: Query<(&Translation,&mut Sprite,&Box,&Name, &mut Handle<ColorMaterial>)>) 
{
    for (translation,mut sprite,_box,name, color) in &mut query.iter() {
        let cursor_tup = translate_cursor ((cursor.0,cursor.1));
        let cursor_vec = Vec3::new(cursor_tup.0,cursor_tup.1,0.0);
        if collides(cursor_vec,Vec2::new(1.0,1.0),translation.0,sprite.size) {
            println!("{}",name.0);
            let mut color_mat = materials.get_mut(&color).unwrap();
            color_mat.color = Color::rgb(1.0,1.0,1.0);
        }
    }
}

因此,您必须获取与实体关联的材料句柄,然后必须获取 ColorMaterialfrom Assets

目前颜色应该只是变成白色。

于 2020-08-24T11:04:28.450 回答