-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasync_asset.rs
52 lines (49 loc) · 1.7 KB
/
async_asset.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use bevy::prelude::*;
use bevy_mod_async::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, AsyncTasksPlugin))
.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 1_000.0,
})
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
Transform::from_xyz(2.0, 1.0, 3.0).looking_at(Vec3::ZERO, Vec3::Y),
));
commands.spawn_task(|cx| async move {
// Spawn in a nice loading screen
let (container, text) = cx
.with_world(|world| {
let container = world
.spawn(Node {
align_self: AlignSelf::Center,
justify_self: JustifySelf::Center,
..default()
})
.id();
let text = world
.spawn((
Text::new("Loading..."),
TextFont {
font_size: 36.0,
..default()
},
))
.set_parent(container)
.id();
(container, text)
})
.await;
let scene = cx.load_asset("FlightHelmet.gltf#Scene0").await.unwrap();
// Now that the scene is loaded, despawn the loading screen and spawn the scene in
// nb we need to despawn each level of the hierarchy, as despawn_recursive isn't available on [World]
cx.despawn(text).detach();
cx.despawn(container).detach();
cx.spawn(SceneRoot(scene)).detach();
});
}