bevy_common_assets

repository·main·Indexed 43 days ago

https://github.com/niklasei/bevy_common_assets

A collection of Bevy plugins providing generic asset loaders and writers for common file formats, including JSON, YAML, TOML, RON, XML, CSV, CBOR, MsgPack, and Postcard. It allows developers to load structured data into Bevy assets by defining types that derive serde::Deserialize, bevy::asset::Asset, and bevy::reflect::TypePath.

Tokens
7.5K
Snippets
24
Records
36
Agent score
54%

What's inside bevy_common_assets

  1. Overview of Bevy Common Assets

    main
    bevy_common_assets is a collection of Bevy plugins that provide generic asset loaders and writers for various common file formats. It allows you to easily load structured data into Bevy assets using formats like JSON, YAML, TOML, and more.
  2. Convert assets using loaders and savers

    main
    You can perform asset transformations (e.g., converting a human-readable JSON asset into a processed, compact Postcard asset) by using the provided AssetLoader and AssetSaver implementations. See the asset_savers example in the repository for a complete implementation.
  3. How to use bevy_common_assets to load data

    main

    To use the asset loaders, follow these steps:

    1. Enable features: Enable the specific feature(s) for the format(s) you intend to use in your Cargo.toml.
    2. Define your data type: Create a struct that represents your data. It must derive serde::Deserialize, bevy::asset::Asset, and bevy::reflect::TypePath.
    3. Add the plugin: Add the corresponding asset plugin to your Bevy App. Plugins are generic over your asset type and allow you to specify custom file extensions via .new(&["extension"]).
    use bevy::prelude::*;
    use bevy_common_assets::cbor::CborAssetPlugin;
    use bevy_common_assets::json::JsonAssetPlugin;
    use bevy_common_assets::msgpack::MsgPackAssetPlugin;
    use bevy_common_assets::postcard::PostcardAssetPlugin;
    use bevy_common_assets::ron::RonAssetPlugin;
    use bevy_common_assets::toml::TomlAssetPlugin;
    use bevy_common_assets::xml::XmlAssetPlugin;
    use bevy_common_assets::yaml::YamlAssetPlugin;
    
    #[derive(serde::Deserialize, bevy::asset::Asset, bevy::reflect::TypePath)]
    struct Level {
        positions: Vec<[f32; 3]>;
    }
    
    fn main() {
        App::new()
            .add_plugins((
                DefaultPlugins,
                JsonAssetPlugin::<Level>::new(&["level.json", "custom.json"]),
                RonAssetPlugin::<Level>::new(&["level.ron"]),
                MsgPackAssetPlugin::<Level>::new(&["level.msgpack"]),
                PostcardAssetPlugin::<Level>::new(&["level.postcard"]),
                TomlAssetPlugin::<Level>::new(&["level.toml"]),
                XmlAssetPlugin::<Level>::new(&["level.xml"]),
                YamlAssetPlugin::<Level>::new(&["level.yaml"]),
                CborAssetPlugin::<Level>::new(&["level.cbor"])
            ))
            .run();
    }
  4. Load assets from RON files using RonAssetPlugin

    main

    To load custom asset types from RON (Rusty Object Notation) files, use the RonAssetPlugin<A>. This plugin registers a RonAssetLoader for your type A. Your type must implement serde::Deserialize, bevy_asset::Asset, and bevy_reflect::TypePath.

    use bevy::prelude::*;
    use bevy_common_assets::ron::RonAssetPlugin;
    use serde::Deserialize;
    use bevy_reflect::TypePath;
    
    #[derive(Deserialize, Asset, TypePath)]
    struct Level {
        value: Option<f32>,
    }
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            .add_plugins(RonAssetPlugin::<Level>::new(&["level.ron"]))
            .run();
    }
  5. Load assets from TOML files using TomlAssetPlugin

    main

    To load a custom asset type A from TOML files, use the TomlAssetPlugin<A>. Your asset type must implement bevy::asset::Asset and serde::Deserialize. You must specify the file extensions (e.g., ["toml"]) that the plugin should associate with this asset type.

    Note: The asset type must be compatible with serde for deserialization.

    use bevy::prelude::*;
    use bevy_common_assets::toml::TomlAssetPlugin;
    use serde::Deserialize;
    
    #[derive(Asset, TypePath, Deserialize, Debug)]
    struct MyConfig {
        value: u32,
    }
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            .add_plugins(TomlAssetPlugin::<MyConfig>::new(&["toml"]))
            .run();
    }
  6. Register a MsgPack asset plugin

    main

    To load custom asset types from MessagePack files, use the MsgPackAssetPlugin<A> plugin. The asset type A must implement bevy::asset::Asset and serde::Deserialize.

    When initializing the plugin, you must provide a slice of file extensions (e.g., &["msgpack"]) that the loader should respond to.

    use bevy::prelude::*;
    use bevy_common_assets::msgpack::MsgPackAssetPlugin;
    
    #[derive(Asset, TypePath, serde::Deserialize, serde::Serialize)]
    struct MyData {
        value: u32,
    }
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            .add_plugins(MsgPackAssetPlugin::<MyData>::new(&["msgpack"]))
            .run();
    }
  7. Load assets from YAML files using YamlAssetPlugin

    main

    To load a custom asset type A from YAML files, use the YamlAssetPlugin<A>. The asset type A must implement bevy::asset::Asset and serde::Deserialize.

    You can specify which file extensions the plugin should respond to (e.g., ["yaml", "yml"]) when initializing the plugin.

    use bevy::prelude::*;
    use bevy_common_assets::yaml::YamlAssetPlugin;
    use serde::Deserialize;
    
    #[derive(Asset, TypePath, Deserialize, Debug)]
    struct MyAsset {
        value: String,
    }
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            .add_plugins(YamlAssetPlugin::<MyAsset>::new(&["yaml", "yml"]))
            .run();
    }
  8. Load custom asset types from CSV files using CsvAssetPlugin

    main

    To load a custom asset type A from CSV files, use the CsvAssetPlugin<A>. Your asset type A must implement serde::Deserialize, bevy_asset::Asset, and bevy_reflect::TypePath.

    When you add this plugin, it registers a LoadedCsv<A> asset type and a CsvAssetLoader<A> that handles the file extensions you specify.

    use bevy::prelude::*;
    use bevy_common_assets::csv::CsvAssetPlugin;
    use bevy_reflect::TypePath;
    
    #[derive(serde::Deserialize, Asset, TypePath, Debug)]
    struct TreePosition {
        x: f32,
        y: f32,
        z: f32,
    }
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            // Register the plugin for TreePosition with .csv extension
            .add_plugins(CsvAssetPlugin::<TreePosition>::new(&["csv"]))
            .run();
    }
  9. Load custom assets from XML using XmlAssetPlugin

    main

    To load a custom asset type A from XML files, use the XmlAssetPlugin<A>. The asset type A must implement bevy::asset::Asset and serde::Deserialize.

    When initializing the plugin, you must provide a slice of file extensions (e.g., &["xml"]) that the loader should respond to. The plugin handles both registering the asset type and registering the XmlAssetLoader with Bevy.

    use bevy::prelude::*;
    use bevy_common_assets::xml::XmlAssetPlugin;
    use serde::Deserialize;
    
    #[derive(Asset, TypePath, Deserialize, Debug)]
    struct MyXmlAsset {
        // your fields here
    }
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            .add_plugins(XmlAssetPlugin::<MyXmlAsset>::new(&["xml"]))
            .run();
    }
  10. Use CborAssetPlugin to load CBOR assets

    main

    The CborAssetPlugin<A> allows you to load custom asset types A from files using the Concise Binary Object Representation (CBOR) format. To use it, your asset type must implement bevy_asset::Asset and serde::Deserialize. You must specify the file extensions that the plugin should associate with this asset type.

    use bevy::prelude::*;
    use bevy_common_assets::cbor::CborAssetPlugin;
    use serde::{Deserialize, Serialize};
    
    #[derive(Asset, TypePath, Serialize, Deserialize, Debug)]
    struct MyCborAsset {
        value: u32,
    }
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            // Register the plugin with specific extensions (e.g., "cbor")
            .add_plugins(CborAssetPlugin::<MyCborAsset>::new(&["cbor"]))
            .run();
    }
  11. Use PostcardAssetPlugin to load assets

    main

    To load custom asset types using the postcard serialization format, use the PostcardAssetPlugin<A> plugin. You must provide a list of file extensions that the plugin should associate with your asset type A.

    Your asset type A must implement bevy_asset::Asset and serde::Deserialize.

    use bevy::prelude::*;
    use bevy_common_assets::postcard::PostcardAssetPlugin;
    
    #[derive(Asset, serde::Deserialize, serde::Serialize, TypePath, Debug)]
    struct MyData {
        value: u32,
    }
    
    fn main() {
        App::new()
            .add_plugins(DefaultPlugins)
            // Register the plugin for MyData with .postcard extensions
            .add_plugins(PostcardAssetPlugin::<MyData>::new(&["postcard"]))
            .run();
    }