#assets #bevy #file-format #yaml #gamedev #json-file

bevy_common_assets

Bevy plugin adding support for loading your own asset types from common file formats such as json and yaml

9 breaking releases

0.10.0 Feb 17, 2024
0.8.0 Nov 4, 2023
0.7.0 Jul 10, 2023
0.6.0 Mar 18, 2023
0.3.0 Jul 30, 2022

#177 in Game dev

Download history 466/week @ 2023-12-23 332/week @ 2023-12-30 561/week @ 2024-01-06 596/week @ 2024-01-13 653/week @ 2024-01-20 455/week @ 2024-01-27 586/week @ 2024-02-03 711/week @ 2024-02-10 1281/week @ 2024-02-17 1283/week @ 2024-02-24 1013/week @ 2024-03-02 827/week @ 2024-03-09 754/week @ 2024-03-16 709/week @ 2024-03-23 1168/week @ 2024-03-30 767/week @ 2024-04-06

3,470 downloads per month
Used in 11 crates (9 directly)

MIT/Apache

40KB
548 lines

Bevy common assets

crates.io docs license crates.io

Collection of Bevy plugins offering generic asset loaders for common file formats.

Supported formats:

format feature example
json json json.rs
msgpack msgpack msgpack.rs
ron ron ron.rs
toml toml toml.rs
xml xml xml.rs
yaml yaml yaml.rs
csv csv csv.rs

Usage

Enable the feature(s) for the format(s) that you want to use.

Define the types that you would like to load from files and derive serde::Deserialize, bevy::reflect::TypePath, and bevy::asset::Asset for them.

#[derive(serde::Deserialize, bevy::asset::Asset, bevy::reflect::TypePath)]
struct Level {
    positions: Vec<[f32;3]>,
}

With the types ready, you can start adding asset plugins. Every plugin gets the asset type that it is supposed to load as a generic parameter. You also need to configure custom file endings for each plugin:

use bevy::prelude::*;
use bevy_common_assets::json::JsonAssetPlugin;
use bevy_common_assets::msgpack::MsgPackAssetPlugin;
use bevy_common_assets::ron::RonAssetPlugin;
use bevy_common_assets::toml::TomlAssetPlugin;
use bevy_common_assets::xml::XmlAssetPlugin;
use bevy_common_assets::yaml::YamlAssetPlugin;

fn main() {
    App::new()
        .add_plugins((
            DefaultPlugins,
            JsonAssetPlugin::<Level>::new(&["level.json", "custom.json"]),
            RonAssetPlugin::<Level>::new(&["level.ron"]),
            MsgPackAssetPlugin::<Level>::new(&["level.msgpack"]),
            TomlAssetPlugin::<Level>::new(&["level.toml"]),
            XmlAssetPlugin::<Level>::new(&["level.xml"]),
            YamlAssetPlugin::<Level>::new(&["level.yaml"])
        ))
        // ...
        .run();
}

#[derive(serde::Deserialize, bevy::asset::Asset, bevy::reflect::TypePath)]
struct Level {
    positions: Vec<[f32; 3]>,
}

The example above will load Level structs from json files ending on .level.json or .custom.json, from ron files ending on .level.ron and so on...

See the examples for working Bevy apps using the different formats.

Compatible Bevy versions

The main branch is compatible with the latest Bevy release.

Compatibility of bevy_common_assets versions:

bevy_common_assets bevy
0.10 0.13
0.8 - 0.9 0.12
0.7 0.11
0.5 - 0.6 0.10
0.4 0.9
0.3 0.8
0.1 - 0.2 0.7
main 0.13
bevy_main main

License

Dual-licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~21–60MB
~1M SLoC