0

在具有以下场景文件的Bevymy_scene.scn项目中:

[
  (
    entity: 0,
    components: [
      {
        "type": "Transform",
        "map": {
          "translation": {
            "type": "Vec3", 
            "value": (150.0, 200.0, 0.0),
          },
          "rotation": {
            "type": "Quat",
            "value": (0.0, 0.0, 0.0, 1.0),
          },
          "scale": {
            "type": "Vec3",
            "value": (0.75, 0.75, 0.0),
          },
        },
      },
    ]
  )
]

并使用此官方 Bevy 示例之后的源代码:

use bevy::prelude::*;

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .add_startup_system(load_scene_system.system())
        .run();
}

fn load_scene_system(asset_server: Res<AssetServer>, mut scene_spawner: ResMut<SceneSpawner>) {
    let scene_handle: Handle<Scene> = asset_server.load("my_scene.scn");

    scene_spawner.spawn_dynamic(scene_handle);
    // ^ How to add components to entities created here ?

    asset_server.watch_for_changes().unwrap();
}

我想向该实体 0 添加其他组件,例如SpriteComponents. 我如何实现这一目标?

4

1 回答 1

3

这是我想出的解决方案。这个想法是存储一个包含信息的组件,以创建一个更动态的组件。

例如,要动态加载场景文件中引用的精灵图像,我们可以定义一个SpriteLoader组件,然后添加一个系统,该系统将用我们想要的组件替换这个加载器组件。

use bevy::prelude::*;

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .register_component::<SpriteLoader>()
        .add_startup_system(load_scene_system.system())
        .add_system(load_sprite_system.system())
        .run();
}

fn load_scene_system(asset_server: Res<AssetServer>, mut scene_spawner: ResMut<SceneSpawner>) {
    let scene_handle: Handle<DynamicScene> = asset_server.load("my_scene.scn");
    scene_spawner.spawn_dynamic(scene_handle);
    asset_server.watch_for_changes().unwrap();
}

/// Component indicating that a sprite will be loaded for the entity.
#[derive(Properties, Default)]
pub struct SpriteLoader {
    /// Path of the sprite to load
    pub path: String
}

/// Replaces the `SpritLoader` component with the corresponding `SpriteComponents`.
pub fn load_sprite_system(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    mut materials: ResMut<Assets<ColorMaterial>>,
    query: Query<(Entity, Added<SpriteLoader>)>,
) {
    for (entity, sprite_loader) in query.iter() {
        commands.remove_one::<SpriteLoader>(entity);

        let path = PathBuf::from(sprite_loader.path.clone());

        commands.insert(
            entity,
            SpriteComponents {
                material: materials.add(asset_server.load(path).into()),
                ..SpriteComponents::default()
            },
        );
    }
}

在这里,一旦将 aSpriteLoader添加到实体中,load_sprite_system系统将删除它并插入 a SpriteComponents代替。根据的文档 insert,这将取代以前的SpriteComponents.

对应的场景文件为:

[
  (
    entity: 0,
    components: [
      {
        "type": "SpriteLoader",
        "map": { 
          "path": "my_sprite.png",
        },
      },
    ]
  )
]
于 2020-11-13T15:48:53.560 回答