我试图查看鼠标按下的位置,以便选择我的角色。我试过以下
#[derive(Default)]
struct State { // Set up from example
mouse_button_event_reader: EventReader<MouseButtonInput>,
cursor_moved_event_reader: EventReader<CursorMoved>,
}
fn select_character(
mut state: ResMut<State>,
mouse_button_input_events: Res<Events<MouseButtonInput>>,
cursor_moved_events: Res<Events<CursorMoved>>,
) {
for (cursor_event, mouse_event) in state
.cursor_moved_event_reader
.iter(&cursor_moved_events)
.zip(
state
.mouse_button_event_reader
.iter(&mouse_button_input_events),
)
{
println!("{:?}", cursor_event);
println!("{:?}", mouse_event);
}
}
这种工作,但鼠标需要在单击时移动。按下鼠标后有没有办法获取位置?
编辑:
我认为.find_latest
可能会起作用,让它返回最新Some
值。
for event in state
.mouse_button_event_reader
.iter(&mouse_button_input_events)
{
let cursor_event = state
.cursor_moved_event_reader
.find_latest(&cursor_moved_events, |x| x.is_Some() // Not sure how to only return values that are Some(x)
);
println!("{:?}", event);
println!("{:?}", cursor_event);
}