Created
June 3, 2025 19:45
-
-
Save ChristopherBiscardi/1d5fa346b8cdbe5854501379d721bafd to your computer and use it in GitHub Desktop.
animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use bevy::{prelude::*, scene::SceneInstanceReady}; | |
fn main() -> AppExit { | |
App::new() | |
.add_plugins(DefaultPlugins) | |
.add_observer( | |
|_trigger: Trigger<SceneInstanceReady>, | |
mut query: Query<( | |
Entity, | |
&mut AnimationPlayer, | |
)>, | |
move_cube: Res<MoveCube>, | |
mut commands: Commands| { | |
info!("here"); | |
for (entity, mut player) in &mut query { | |
// commenting out this line "breaks" the player | |
commands.entity(entity).insert( | |
AnimationGraphHandle( | |
move_cube.0.clone(), | |
), | |
); | |
player.play(1.into()); | |
info!("a player"); | |
} | |
}, | |
) | |
.add_systems(Update, gltfs) | |
.add_systems(Startup, startup) | |
.run() | |
} | |
#[derive(Resource)] | |
struct G(Handle<Gltf>); | |
#[derive(Resource)] | |
struct MoveCube(Handle<AnimationGraph>); | |
fn startup( | |
mut commands: Commands, | |
asset_server: Res<AssetServer>, | |
) { | |
let g = asset_server.load("boxy.gltf"); | |
commands.insert_resource(G(g)); | |
} | |
fn gltfs( | |
mut reader: EventReader<AssetEvent<Gltf>>, | |
gltfs: Res<Assets<Gltf>>, | |
mut commands: Commands, | |
mut animation_graphs: ResMut<Assets<AnimationGraph>>, | |
) { | |
for event in reader.read() { | |
let AssetEvent::LoadedWithDependencies { id } = | |
event | |
else { | |
continue; | |
}; | |
info!(?event, "event"); | |
let Some(gltf) = gltfs.get(*id) else { continue }; | |
info!(animations = ?gltf.named_animations); | |
let move_cube = gltf | |
.named_animations | |
.get("MoveCube") | |
.unwrap() | |
.clone(); | |
let mut animation_graph = AnimationGraph::new(); | |
animation_graph.add_clip( | |
move_cube, | |
1.0, | |
animation_graph.root, | |
); | |
let animation_graph = | |
animation_graphs.add(animation_graph); | |
commands.insert_resource(MoveCube(animation_graph)); | |
commands.spawn(SceneRoot( | |
gltf.named_scenes.get("Scene").unwrap().clone(), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment