You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bevy中使用WindowDescriptor报错:未实现Resource trait求助

解决Bevy中WindowDescriptor无法作为Resource插入的编译错误

我按照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();
}

运行时出现编译错误:

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`

错误原因

这是Bevy版本迭代导致的API变更:教程视频使用的是Bevy 0.10及更早版本,而当前使用的是0.11+版本。在0.11版本中,WindowDescriptor被移除,替换为Window结构体,且它不再实现Resource trait,因此无法通过insert_resource方法插入。

解决办法

方案1:适配Bevy 0.11+版本写法

使用WindowPlugin配置初始窗口属性,替换原有的insert_resource调用:

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins.set(WindowPlugin {
            primary_window: Some(Window {
                resolution: (140.0, 140.0).into(),
                title: "Game of Life".to_string(),
                ..Default::default()
            }),
            ..Default::default()
        }))
        .run();
}

若需在运行时动态修改窗口属性,可直接插入Window资源(初始化阶段优先用插件配置)。

方案2:降级到教程对应Bevy版本

如果想完全沿用教程代码,可在Cargo.toml中指定旧版本Bevy,例如0.10.1:

[dependencies]
bevy = "0.10.1"

内容的提问来源于stack exchange,提问作者Sarang Dutta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 04:30:21