1

假设我有这样的事情:

use bevy::prelude::*;

// Bevy style tag
struct &CharacterBox;

// Somewhere to store the entity
pub struct Action {
    pub character_box: Option<Entity>,
};

fn setup( mut commands: Commands, mut action: ResMut<Action> ) {
    if let Some(entity) = commands
        .spawn(UiCameraComponents::default())
        .spawn(NodeComponents { /* snip */ })
        .with_children(|p| {
            p.spawn(ButtonComponents { /* snip, snap */ });
        })
        .with(CharacterBox)
        .current_entity()
    {
        action.character_box = Some(entity);
    }
}

一个带有一两个按钮的 NodeComponents 从启动...

...后来我想从我添加的系统中添加更多按钮:

fn do_actions(
    mut commands: Commands,
    action: ChangedRes<Action>,
    mut query: Query<(&CharacterBox, &Children)>,
) {

    if let Some(entity) = commands
        .spawn(ButtonComponents { /* ... */ })
        .current_entity()
    {
        let mut charbox = query.get_mut::<Children>(action.character_box.unwrap()).unwrap();
        // I know this is naïve, I know I can't just push in the entity,
        // but it illustrates my point...
        charbox.push(entity); // How do I achieve this?
    }

}

如何将我生成的实体(组件?)插入我的 NodeComponents.Children?

如何将组件生成到已经存在的组件中?

或者我如何访问 NodeComponents.Children.ChildBuilder?我可以查询 ChildBuilders 吗?

编辑:删除的编辑。

4

1 回答 1

1

无论如何,这是我解决它的方法:

 let parent_entity = action.character_box.unwrap();
 let new_entity = commands
      .spawn(ButtonComponents { /* ... */ })
      .current_entity()
      .unwrap();

 commands.push_children(parent_entity, &[c]);

(使用 nested NodeComponents,我必须单独生成它们,然后将每个实体推入另一个实体,因为据我所知,没有办法从 ChildBuilder 中获取实体,只能使用commands.spawn(...).current_entity(). )

于 2020-10-21T04:38:52.490 回答