-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AssetPack syntax sugar for static assets #13819
base: main
Are you sure you want to change the base?
Conversation
also allow any Expr to be used in the `load` attribute
The generated |
Instead of AssetPackPlugin, you could just provide a register_asset_pack fn. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried running the example but got
$ RUST_BACKTRACE=1 cargo run --example asset_pack
[…]
thread 'main' panicked at crates/bevy_asset/src/io/embedded/mod.rs:141:13:
Failed to find src_prefix "src" in "examples/asset/asset_pack.rs"
Maybe this error message can be improved? The file exists, and I have no idea why it's failing.
fn main() { | ||
App::new() | ||
.add_plugins((DefaultPlugins, AssetPackExamplePlugin)) | ||
.add_systems(Startup, setup) | ||
.run(); | ||
} | ||
|
||
struct AssetPackExamplePlugin; | ||
|
||
#[derive(AssetPack)] | ||
struct ExampleAssetPack { | ||
#[embedded("files/bevy_pixel_dark.png")] | ||
sprite: Handle<Image>, | ||
} | ||
|
||
impl Plugin for AssetPackExamplePlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_plugins(AssetPackPlugin::<ExampleAssetPack>::default()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn main() { | |
App::new() | |
.add_plugins((DefaultPlugins, AssetPackExamplePlugin)) | |
.add_systems(Startup, setup) | |
.run(); | |
} | |
struct AssetPackExamplePlugin; | |
#[derive(AssetPack)] | |
struct ExampleAssetPack { | |
#[embedded("files/bevy_pixel_dark.png")] | |
sprite: Handle<Image>, | |
} | |
impl Plugin for AssetPackExamplePlugin { | |
fn build(&self, app: &mut App) { | |
app.add_plugins(AssetPackPlugin::<ExampleAssetPack>::default()); | |
} | |
} | |
fn main() { | |
App::new() | |
.add_plugins(DefaultPlugins) | |
.add_plugins(AssetPackPlugin::<ExampleAssetPack>::default()) | |
.add_systems(Startup, setup) | |
.run(); | |
} | |
#[derive(AssetPack)] | |
struct ExampleAssetPack { | |
#[embedded("files/bevy_pixel_dark.png")] | |
sprite: Handle<Image>, | |
} |
No need for a plugin, let's keep this example short :)
Objective
Currently there's not a ton of boilerplate for loading assets, either using
embedded_asset!
or not. But this can still add up, and does especially in rendering code.Solution
Limitations
Currently, all assets are loaded at once in
Pack<T>::get()
and never unloaded. This fits the need of rendering (and I assume other cases as well), where each shader is needed every frame and each pack is accessed (usually) all at once. Loading assets separately and unloading assets when unused would require a function-like proc-macro to be able to rewrite each struct field. For this PR I decided to continue with the simpler implementation.Testing
I did some quick manual testing to make sure the macro worked as expected. Frankly, I'm not super familiar with macro testing or style for error reporting. Please let me know how to improve this!
Changelog
AssetPack
trait and derive macroAssetPackPlugin<T>
to setup asset pack statePack<T>
resource to wrap access to anAssetPack
GetPack<T>
SystemParam so users don't have to pass inAssetServer
themselvesasset_pack
example and docs