Skip to content

Default Mesh3d with Bevy 0.15 require components #18946

Answered by Henauxg
TanJunKiat asked this question in Q&A
Discussion options

You must be logged in to vote

While you can't access the Assets resources in a default constructor, you can achieve what you want with Component Hooks. Here's an example:

#[derive(Component)]
#[require(Transform, MeshMaterial3d<StandardMaterial>)]
#[component(on_add = add_default_cube)]
struct CustomComponent;

pub fn add_default_cube(mut world: DeferredWorld<'_>, HookContext { entity, .. }: HookContext) {
    // Retrieve a mesh handle. This handle could also be retrieved from a Resource instead of created here
    let Some(mut meshes) = world.get_resource_mut::<Assets<Mesh>>() else {
        return;
    };
    let mesh_handle = meshes.add(Cuboid::from_length(1.));
    // Add the component
    world.commands().entity(e…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@TanJunKiat
Comment options

Answer selected by TanJunKiat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants