# Objective
- Spawning a scene is handled as a special case with a command `spawn_scene` that takes an handle but doesn't let you specify anything else. This is the only handle that works that way.
- Workaround for this have been to add the `spawn_scene` on `ChildBuilder` to be able to specify transform of parent, or to make the `SceneSpawner` available to be able to select entities from a scene by their instance id
## Solution
Add a bundle
```rust
pub struct SceneBundle {
pub scene: Handle<Scene>,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub instance_id: Option<InstanceId>,
}
```
and instead of
```rust
commands.spawn_scene(asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"));
```
you can do
```rust
commands.spawn_bundle(SceneBundle {
scene: asset_server.load("models/FlightHelmet/FlightHelmet.gltf#Scene0"),
..Default::default()
});
```
The scene will be spawned as a child of the entity with the `SceneBundle`
~I would like to remove the command `spawn_scene` in favor of this bundle but didn't do it yet to get feedback first~
Co-authored-by: François <8672791+mockersf@users.noreply.github.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
21 lines
694 B
Rust
21 lines
694 B
Rust
use bevy_ecs::world::World;
|
|
use bevy_reflect::TypeUuid;
|
|
|
|
/// To spawn a scene, you can use either:
|
|
/// * [`SceneSpawner::spawn`](crate::SceneSpawner::spawn)
|
|
/// * adding the [`SceneBundle`](crate::SceneBundle) to an entity
|
|
/// * adding the [`Handle<Scene>`](bevy_asset::Handle) to an entity (the scene will only be
|
|
/// visible if the entity already has [`Transform`](bevy_transform::components::Transform) and
|
|
/// [`GlobalTransform`](bevy_transform::components::GlobalTransform) components)
|
|
#[derive(Debug, TypeUuid)]
|
|
#[uuid = "c156503c-edd9-4ec7-8d33-dab392df03cd"]
|
|
pub struct Scene {
|
|
pub world: World,
|
|
}
|
|
|
|
impl Scene {
|
|
pub fn new(world: World) -> Self {
|
|
Self { world }
|
|
}
|
|
}
|