//! Example of loading an embedded asset.12//! An embedded asset is an asset included in the program's memory, in contrast to other assets that are normally loaded from disk to memory when needed.3//! The below example embeds the asset at program startup, unlike the common use case of embedding an asset at build time. Embedded an asset at program startup can be useful4//! for things like loading screens, since it might be nice to display some art while other, non-embedded, assets are loading.56//! One common use case for embedded assets is including them directly within the executable during its creation. By embedding an asset at build time rather than runtime7//! the program never needs to go to disk for the asset at all, since it is already located in the program's binary executable.8use bevy::{9asset::{embedded_asset, io::AssetSourceId, AssetPath},10prelude::*,11};12use std::path::Path;1314fn main() {15App::new()16.add_plugins((DefaultPlugins, EmbeddedAssetPlugin))17.add_systems(Startup, setup)18.run();19}2021struct EmbeddedAssetPlugin;2223impl Plugin for EmbeddedAssetPlugin {24fn build(&self, app: &mut App) {25// We get to choose some prefix relative to the workspace root which26// will be ignored in "embedded://" asset paths.27let omit_prefix = "examples/asset";28// Path to asset must be relative to this file, because that's how29// include_bytes! works.30embedded_asset!(app, omit_prefix, "files/bevy_pixel_light.png");31}32}3334fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {35commands.spawn(Camera2d);3637// Each example is its own crate (with name from [[example]] in Cargo.toml).38let crate_name = "embedded_asset";3940// The actual file path relative to workspace root is41// "examples/asset/files/bevy_pixel_light.png".42//43// We omit the "examples/asset" from the embedded_asset! call and replace it44// with the crate name.45let path = Path::new(crate_name).join("files/bevy_pixel_light.png");46let source = AssetSourceId::from("embedded");47let asset_path = AssetPath::from_path(&path).with_source(source);4849// You could also parse this URL-like string representation for the asset50// path.51assert_eq!(52asset_path,53"embedded://embedded_asset/files/bevy_pixel_light.png".into()54);5556commands.spawn(Sprite::from_image(asset_server.load(asset_path)));57}585960