使用Bevy,如何在创建后获取并设置Window信息?

问题描述

我希望能够使用Bevy读取和设置窗口设置。我尝试使用基本系统来做到这一点:

fn test_system(mut win_desc: ResMut<WindowDescriptor>) {
    win_desc.title = "test".to_string();
    println!("{}",win_desc.title);
}

虽然这部分可行,但它只为您提供原始设置,并且根本不允许更改。在此示例中,标题将保持不变,但标题显示将发生变化。在另一个示例中,如果要打印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);
    }
}

其他有用的事件可以在以下位置找到 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);
}

zyrg所述,您还可以监听窗口事件。