//! This example illustrates various ways to load assets.12use bevy::{asset::LoadedFolder, prelude::*};34fn main() {5App::new()6.add_plugins(DefaultPlugins)7.add_systems(Startup, setup)8.run();9}1011fn setup(12mut commands: Commands,13asset_server: Res<AssetServer>,14meshes: Res<Assets<Mesh>>,15mut materials: ResMut<Assets<StandardMaterial>>,16) {17// By default AssetServer will load assets from inside the "assets" folder.18// For example, the next line will load GltfAssetLabel::Primitive{mesh:0,primitive:0}.from_asset("ROOT/assets/models/cube/cube.gltf"),19// where "ROOT" is the directory of the Application.20//21// This can be overridden by setting [`AssetPlugin.file_path`].22let cube_handle = asset_server.load(23GltfAssetLabel::Primitive {24mesh: 0,25primitive: 0,26}27.from_asset("models/cube/cube.gltf"),28);29let sphere_handle = asset_server.load(30GltfAssetLabel::Primitive {31mesh: 0,32primitive: 0,33}34.from_asset("models/sphere/sphere.gltf"),35);3637// All assets end up in their Assets<T> collection once they are done loading:38if let Some(sphere) = meshes.get(&sphere_handle) {39// You might notice that this doesn't run! This is because assets load in parallel without40// blocking. When an asset has loaded, it will appear in relevant Assets<T>41// collection.42info!("{:?}", sphere.primitive_topology());43} else {44info!("sphere hasn't loaded yet");45}4647// You can load all assets in a folder like this. They will be loaded in parallel without48// blocking. The LoadedFolder asset holds handles to each asset in the folder. These are all49// dependencies of the LoadedFolder asset, meaning you can wait for the LoadedFolder asset to50// fire AssetEvent::LoadedWithDependencies if you want to wait for all assets in the folder51// to load.52// If you want to keep the assets in the folder alive, make sure you store the returned handle53// somewhere.54let _loaded_folder: Handle<LoadedFolder> = asset_server.load_folder("models/torus");5556// If you want a handle to a specific asset in a loaded folder, the easiest way to get one is to call load.57// It will _not_ be loaded a second time.58// The LoadedFolder asset will ultimately also hold handles to the assets, but waiting for it to load59// and finding the right handle is more work!60let torus_handle = asset_server.load(61GltfAssetLabel::Primitive {62mesh: 0,63primitive: 0,64}65.from_asset("models/torus/torus.gltf"),66);6768// You can also add assets directly to their Assets<T> storage:69let material_handle = materials.add(StandardMaterial {70base_color: Color::srgb(0.8, 0.7, 0.6),71..default()72});7374// torus75commands.spawn((76Mesh3d(torus_handle),77MeshMaterial3d(material_handle.clone()),78Transform::from_xyz(-3.0, 0.0, 0.0),79));80// cube81commands.spawn((82Mesh3d(cube_handle),83MeshMaterial3d(material_handle.clone()),84Transform::from_xyz(0.0, 0.0, 0.0),85));86// sphere87commands.spawn((88Mesh3d(sphere_handle),89MeshMaterial3d(material_handle),90Transform::from_xyz(3.0, 0.0, 0.0),91));92// light93commands.spawn((PointLight::default(), Transform::from_xyz(4.0, 5.0, 4.0)));94// camera95commands.spawn((96Camera3d::default(),97Transform::from_xyz(0.0, 3.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),98));99}100101102