如何在 Bevy 中使用 Windowdescriptor?

Sar*_*tta 3 rust bevy

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .insert_resource(WindowDescriptor{
            width: 140.0,
            height:140.0,
            title: "Game of Life".to_string(),
            ..Default::default()
        })
        .run();
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么这不起作用并且显示以下编译错误

error[E0277]: the trait bound `bevy::prelude::WindowDescriptor: Resource` is not satisfied                                                                                             
   --> src\main.rs:7:26
    |
7   |           .insert_resource(WindowDescriptor{
    |  __________---------------_^
    | |          |
    | |          required by a bound introduced by this call
8   | |             width: 140.0,
9   | |             height:140.0,
10  | |             title: "Game of Life".to_string(),
11  | |             ..Default::default()
12  | |         })
    | |_________^ the trait `Resource` is not implemented for `bevy::prelude::WindowDescriptor`
Run Code Online (Sandbox Code Playgroud)

我正在关注这个视频,它在视频中运行得很好

我正在学习如何使用 bevy 游戏引擎,但是当我将 windowdesciptor 作为参数传递给 add_resource 函数时,它显示错误

小智 10

在 bevy 0.10 中,他们再次更改了一些内容,因此尽管这是一篇较旧的帖子,但我会留下更新的答案。

现在的代码应该是:

use bevy::prelude::*;

fn main() {
  App::new()
    .add_plugins(WindowPlugin {
      primary_window: Some(Window {
        resolution: (140.0, 140.0).into(),
        title: "Game of Life".to_string(),
        ..default()
      }),
      ..default()
    })
    .run();
}
Run Code Online (Sandbox Code Playgroud)


小智 5

从 0.9 开始

设置WindowDescriptor已从资源移至WindowPlugin::window

所以你的代码应该是:

use bevy::prelude::*;

fn main() {
  App::new()
    .add_plugins(DefaultPlugins.set(WindowPlugin {
      window: WindowDescriptor { 
        width: 140.0,
        height:140.0,
        title: "Game of Life".to_string(),
        ..default()
      },
      ..default()
    }))
    .run();
}
Run Code Online (Sandbox Code Playgroud)