我希望能够使用 Bevy 读取和设置窗口设置。我试图用一个基本系统来做到这一点:
fn test_system(mut win_desc: ResMut<WindowDescriptor>) {
win_desc.title = "test".to_string();
println!("{}",win_desc.title);
}
Run Code Online (Sandbox Code Playgroud)
虽然这(部分)有效,但它只为您提供原始设置,并且根本不允许更改。在这个例子中,标题不会改变,但标题的显示会改变。在另一个示例中,如果您要打印,则不会反映更改窗口大小(在运行时手动)win_desc.width。
目前,WindowDescriptor 仅在窗口创建期间使用,以后不会更新
要在调整窗口大小时收到通知,我使用以下系统:
fn resize_notificator(resize_event: Res<Events<WindowResized>>) {
let mut reader = resize_event.get_reader();
for e in reader.iter(&resize_event) {
println!("width = {} height = {}", e.width, e.height);
}
}
Run Code Online (Sandbox Code Playgroud)
其他有用的事件可以在https://github.com/bevyengine/bevy/blob/master/crates/bevy_window/src/event.rs找到
bevy GitHub 存储库中提供了一个使用 Windows 的很好的示例:https://github.com/bevyengine/bevy/blob/master/examples/window/window_settings.rs
对于您的读/写窗口大小的情况:
fn window_resize_system(mut windows: ResMut<Windows>) {
let window = windows.get_primary_mut().unwrap();
println!("Window size was: {},{}", window.width(), window.height());
window.set_resolution(1280, 720);
}
Run Code Online (Sandbox Code Playgroud)
正如zyrg提到的,您还可以监听窗口事件。
使用 Bevy 0.10,您可以执行以下操作:
fn get_window(window: Query<&Window>) {
let window = window.single();
let width = window.resolution.width();
let height = window.resolution.height();
let (x, y) = match window.position {
WindowPosition::At(v) => (v.x as f32, v.y as f32),
_ => (0., 0.),
};
dbg!(width, height, x, y);
}
Run Code Online (Sandbox Code Playgroud)