-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcurrent_thread_runtime.rs
49 lines (44 loc) · 1.59 KB
/
current_thread_runtime.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
use std::time::Duration;
use bevy::color::Srgba;
use bevy::prelude::{App, Camera2d, ClearColor, Commands, DefaultPlugins, ResMut};
use bevy_app::Startup;
use bevy_tokio_tasks::TokioTasksRuntime;
static COLORS: [Srgba; 5] = [
bevy::color::palettes::css::RED,
bevy::color::palettes::css::GREEN,
bevy::color::palettes::css::BLUE,
bevy::color::palettes::css::WHITE,
bevy::color::palettes::css::BLACK,
];
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(bevy_tokio_tasks::TokioTasksPlugin {
make_runtime: Box::new(|| {
let mut runtime = tokio::runtime::Builder::new_current_thread();
runtime.enable_all();
runtime.build().unwrap()
}),
..bevy_tokio_tasks::TokioTasksPlugin::default()
})
.add_systems(Startup, demo)
.run();
}
fn demo(runtime: ResMut<TokioTasksRuntime>, mut commands: Commands) {
commands.spawn(Camera2d);
runtime.spawn_background_task(|mut ctx| async move {
let mut color_index = 0;
loop {
println!("Loop start");
ctx.run_on_main_thread(move |ctx| {
if let Some(mut clear_color) = ctx.world.get_resource_mut::<ClearColor>() {
clear_color.0 = bevy::prelude::Color::Srgba(COLORS[color_index]);
println!("Changed clear color to {:?}", clear_color.0);
}
})
.await;
color_index = (color_index + 1) % COLORS.len();
tokio::time::sleep(Duration::from_secs(1)).await;
}
});
}