2024-03-06 16:00:03 +00:00
|
|
|
//! An example of registering an extra asset source, and loading assets from it.
|
|
|
|
//! This asset source exists in addition to the default asset source.
|
|
|
|
|
2024-09-24 11:42:59 +00:00
|
|
|
use bevy::{
|
|
|
|
asset::{
|
|
|
|
io::{AssetSourceBuilder, AssetSourceId},
|
|
|
|
AssetPath,
|
|
|
|
},
|
|
|
|
prelude::*,
|
2024-03-06 16:00:03 +00:00
|
|
|
};
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
fn main() {
|
2024-03-07 05:44:52 +00:00
|
|
|
App::new()
|
|
|
|
// Add an extra asset source with the name "example_files" to
|
|
|
|
// AssetSourceBuilders.
|
|
|
|
//
|
|
|
|
// This must be done before AssetPlugin finalizes building assets.
|
|
|
|
.register_asset_source(
|
|
|
|
"example_files",
|
|
|
|
AssetSourceBuilder::platform_default("examples/asset/files", None),
|
|
|
|
)
|
|
|
|
// DefaultPlugins contains AssetPlugin so it must be added to our App
|
|
|
|
// after inserting our new asset source.
|
|
|
|
.add_plugins(DefaultPlugins)
|
2024-03-06 16:00:03 +00:00
|
|
|
.add_systems(Startup, setup)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
2024-10-05 01:59:52 +00:00
|
|
|
commands.spawn(Camera2d);
|
2024-03-06 16:00:03 +00:00
|
|
|
|
2024-03-07 05:44:52 +00:00
|
|
|
// Now we can load the asset using our new asset source.
|
2024-03-06 16:00:03 +00:00
|
|
|
//
|
|
|
|
// The actual file path relative to workspace root is
|
|
|
|
// "examples/asset/files/bevy_pixel_light.png".
|
|
|
|
let path = Path::new("bevy_pixel_light.png");
|
|
|
|
let source = AssetSourceId::from("example_files");
|
|
|
|
let asset_path = AssetPath::from_path(path).with_source(source);
|
|
|
|
|
|
|
|
// You could also parse this URL-like string representation for the asset
|
|
|
|
// path.
|
|
|
|
assert_eq!(asset_path, "example_files://bevy_pixel_light.png".into());
|
|
|
|
|
|
|
|
commands.spawn(SpriteBundle {
|
|
|
|
texture: asset_server.load(asset_path),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
}
|