-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix embedded asset path manipulation (#10383)
# Objective Fixes #10377 ## Solution Use `Path::strip_prefix` instead of `str::split`. Avoid any explicit "/" characters in path manipulation. --- ## Changelog - Added: example of embedded asset loading - Added: support embedded assets in external crates - Fixed: resolution of embedded assets - Fixed: unexpected runtime panic during asset path resolution ## Migration Guide No API changes. --------- Co-authored-by: Shane Celis <[email protected]>
- Loading branch information
1 parent
6f2eec8
commit 176223b
Showing
5 changed files
with
209 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//! Example of loading an embedded asset. | ||
|
||
use bevy::asset::{embedded_asset, io::AssetSourceId, AssetPath}; | ||
use bevy::prelude::*; | ||
use std::path::Path; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins((DefaultPlugins, EmbeddedAssetPlugin)) | ||
.add_systems(Startup, setup) | ||
.run(); | ||
} | ||
|
||
struct EmbeddedAssetPlugin; | ||
|
||
impl Plugin for EmbeddedAssetPlugin { | ||
fn build(&self, app: &mut App) { | ||
// We get to choose some prefix relative to the workspace root which | ||
// will be ignored in "embedded://" asset paths. | ||
let omit_prefix = "examples/asset"; | ||
// Path to asset must be relative to this file, because that's how | ||
// include_bytes! works. | ||
embedded_asset!(app, omit_prefix, "bevy_pixel_light.png"); | ||
} | ||
} | ||
|
||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { | ||
commands.spawn(Camera2dBundle::default()); | ||
|
||
// Each example is its own crate (with name from [[example]] in Cargo.toml). | ||
let crate_name = "embedded_asset"; | ||
|
||
// The actual file path relative to workspace root is | ||
// "examples/asset/bevy_pixel_light.png". | ||
// | ||
// We omit the "examples/asset" from the embedded_asset! call and replace it | ||
// with the crate name. | ||
let path = Path::new(crate_name).join("bevy_pixel_light.png"); | ||
let source = AssetSourceId::from("embedded"); | ||
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, | ||
"embedded://embedded_asset/bevy_pixel_light.png".into() | ||
); | ||
|
||
commands.spawn(SpriteBundle { | ||
texture: asset_server.load(asset_path), | ||
..default() | ||
}); | ||
} |