如何在Bevy中添加窗口图标?



我正在制作一款游戏,我希望它是合法的。Bevy ECS很棒。我一直在跟随教程并阅读文档,但有一件事我想知道。我可以更改窗口图标吗?如果有,怎么做?

这并不容易做到。你可以在这里看到关于这个问题的讨论,还有这里和这里。我相信它很快就会以一种很好的标准方式得到解决,同时这里有一种hack的方法来实现它

use bevy::window::WindowId;
use bevy::winit::WinitWindows;
use winit::window::Icon;
fn set_window_icon(
// we have to use `NonSend` here
windows: NonSend<WinitWindows>,
) {
let primary = windows.get_window(WindowId::primary()).unwrap();
// here we use the `image` crate to load our icon data from a png file
// this is not a very bevy-native solution, but it will do
let (icon_rgba, icon_width, icon_height) = {
let image = image::open("my_icon.png")
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
let icon = Icon::from_rgba(icon_rgba, icon_width, icon_height).unwrap();
primary.set_window_icon(Some(icon));
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(set_window_icon)
.run();
}

重0.10 ~ 0.11

Cargo.toml

[dependencies]
bevy = "0.11.0"
image = "*"
winit = "0.28.6"

systems.rs

use bevy::winit::WinitWindows;
use bevy::{prelude::*, window::PrimaryWindow};
use winit::window::Icon;
pub fn set_window_icon(
main_window: Query<Entity, With<PrimaryWindow>>,
windows: NonSend<WinitWindows>,
) {
let Some(primary) = windows.get_window(main_window.single()) else {return};
let (icon_rgba, icon_width, icon_height) = {
let image = image::open("icon.ico")
.expect("Failed to open icon path")
.into_rgba8();
let (width, height) = image.dimensions();
let rgba = image.into_raw();
(rgba, width, height)
};
let icon = Icon::from_rgba(icon_rgba, icon_width, icon_height).unwrap();
primary.set_window_icon(Some(icon));
}

相关内容

  • 没有找到相关文章

最新更新