Skip to content

Commit

Permalink
Add some documentation to the processing example
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Sep 7, 2023
1 parent ca328ad commit 1e0281d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/asset/processing/assets/a.cool.ron.meta
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
settings: (
loader_settings: (),
saver_settings: (
appended: "",
appended: "X",
),
),
),
Expand Down
40 changes: 35 additions & 5 deletions examples/asset/processing/processing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! This example illustrates how to define custom AssetLoaders and AssetSavers, how to configure them, and how to register asset processors.
use bevy::{
asset::{
io::{Reader, Writer},
Expand All @@ -23,20 +25,36 @@ fn main() {
"examples/asset/processing/imported_assets".to_string(),
),
)
// Enabling `processed_dev` will configure the AssetPlugin to use asset processing.
// This will run the AssetProcessor in the background, which will listen for changes to
// the `assets` folder, run them through configured asset processors, and write the results
// to the `imported_assets` folder.
//
// The AssetProcessor will create `.meta` files automatically for assets in the `assets` folder,
// which can then be used to configure how the asset will be processed.
.add_plugins((DefaultPlugins.set(AssetPlugin::processed_dev()), TextPlugin))
// This is what a deployed app should use
// .add_plugins((DefaultPlugins.set(AssetPlugin::processed()), TextPlugin))
.add_systems(Startup, setup)
.add_systems(Update, print_text)
.run();
}

/// The TextPlugin will define two assets types:
/// * CoolText: a custom RON text format that supports dependencies and embedded dependencies
/// * Text: a "normal" plain text file
///
/// It also defines an asset processor that will load CoolText, resolve embedded dependencies, and write the resulting
/// output to a "normal" plain text file. When the processed asset is loaded, it is loaded as a Text (plaintext) asset.
/// This illustrates that when you process an asset, you can change its type! However you don't _need_ to change the type.
pub struct TextPlugin;

impl Plugin for TextPlugin {
fn build(&self, app: &mut App) {
app.init_asset::<Text>()
.init_asset::<CoolText>()
.register_asset_loader(TextLoader)
app.init_asset::<CoolText>()
.init_asset::<Text>()
.register_asset_loader(CoolTextLoader)
.register_asset_loader(TextLoader)
.register_asset_processor::<LoadAndSave<CoolTextLoader, CoolTextSaver>>(
CoolTextSaver.into(),
)
Expand Down Expand Up @@ -164,17 +182,29 @@ impl AssetSaver for CoolTextSaver {
#[derive(Resource)]
struct TextAssets {
a: Handle<Text>,
b: Handle<Text>,
c: Handle<Text>,
d: Handle<Text>,
}

fn setup(mut commands: Commands, assets: Res<AssetServer>) {
// This the final processed versions of `assets/a.cool.ron` and `assets/foo.c.cool.ron`
// Check out their counterparts in `imported_assets` to see what the outputs look like.
commands.insert_resource(TextAssets {
a: assets.load("a.cool.ron"),
b: assets.load("foo/b.cool.ron"),
c: assets.load("foo/c.cool.ron"),
d: assets.load("d.cool.ron"),
});
}

fn print_text(handles: Res<TextAssets>, texts: Res<Assets<Text>>) {
println!("a {:?}", texts.get(&handles.a));
println!("c {:?}", texts.get(&handles.c));
// This prints the current values of the assets
// Hot-reloading is supported, so try modifying the source assets (and their meta files)!
println!("Current Values:");
println!(" a: {:?}", texts.get(&handles.a));
println!(" b: {:?}", texts.get(&handles.b));
println!(" c: {:?}", texts.get(&handles.c));
println!(" d: {:?}", texts.get(&handles.d));
println!();
}

0 comments on commit 1e0281d

Please sign in to comment.