Default Mesh3d with Bevy 0.15 require
components
#18946
-
The new I want to create an entity where it has a default Right now, I am struggling to define a #[derive(Component)]
#[require(Transform, Mesh3d(default_mesh))]
struct Parent;
fn default_mesh() -> Mesh3d {
let mut mesh = Mesh3d::default();
/// what to do here
return mesh;
} What i am familiar with is using the assets server to spawn meshes. Not sure how should I go about doing this. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
While you can't access the #[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(entity).insert(Mesh3d(mesh_handle));
} Now simply spawning cmd.spawn(CustomComponent); I added (EDIT: you can also use an Observer for a similar effect) |
Beta Was this translation helpful? Give feedback.
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: