bevy_common_assets
repository·main·Indexed 43 days ago
https://github.com/niklasei/bevy_common_assetsA 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.
What's inside bevy_common_assets
- 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.
Convert assets using loaders and savers
mainYou can perform asset transformations (e.g., converting a human-readable JSON asset into a processed, compact Postcard asset) by using the providedAssetLoaderandAssetSaverimplementations. See theasset_saversexample in the repository for a complete implementation.How to use bevy_common_assets to load data
mainTo use the asset loaders, follow these steps:
- Enable features: Enable the specific feature(s) for the format(s) you intend to use in your
Cargo.toml. - Define your data type: Create a struct that represents your data. It must derive
serde::Deserialize,bevy::asset::Asset, andbevy::reflect::TypePath. - 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(); }- Enable features: Enable the specific feature(s) for the format(s) you intend to use in your
Load assets from RON files using RonAssetPlugin
mainTo load custom asset types from RON (Rusty Object Notation) files, use the
RonAssetPlugin<A>. This plugin registers aRonAssetLoaderfor your typeA. Your type must implementserde::Deserialize,bevy_asset::Asset, andbevy_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(); }Load assets from TOML files using TomlAssetPlugin
mainTo load a custom asset type
Afrom TOML files, use theTomlAssetPlugin<A>. Your asset type must implementbevy::asset::Assetandserde::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
serdefor 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(); }Register a MsgPack asset plugin
mainTo load custom asset types from MessagePack files, use the
MsgPackAssetPlugin<A>plugin. The asset typeAmust implementbevy::asset::Assetandserde::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(); }Load assets from YAML files using YamlAssetPlugin
mainTo load a custom asset type
Afrom YAML files, use theYamlAssetPlugin<A>. The asset typeAmust implementbevy::asset::Assetandserde::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(); }Load custom asset types from CSV files using CsvAssetPlugin
mainTo load a custom asset type
Afrom CSV files, use theCsvAssetPlugin<A>. Your asset typeAmust implementserde::Deserialize,bevy_asset::Asset, andbevy_reflect::TypePath.When you add this plugin, it registers a
LoadedCsv<A>asset type and aCsvAssetLoader<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(); }Load custom assets from XML using XmlAssetPlugin
mainTo load a custom asset type
Afrom XML files, use theXmlAssetPlugin<A>. The asset typeAmust implementbevy::asset::Assetandserde::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 theXmlAssetLoaderwith 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(); }Use CborAssetPlugin to load CBOR assets
mainThe
CborAssetPlugin<A>allows you to load custom asset typesAfrom files using the Concise Binary Object Representation (CBOR) format. To use it, your asset type must implementbevy_asset::Assetandserde::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(); }Use PostcardAssetPlugin to load assets
mainTo load custom asset types using the
postcardserialization format, use thePostcardAssetPlugin<A>plugin. You must provide a list of file extensions that the plugin should associate with your asset typeA.Your asset type
Amust implementbevy_asset::Assetandserde::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(); }Supported file formats in bevy_common_assets
mainThe library supports the following formats:
format feature jsonjsonmsgpackmsgpackpostcardpostcardronrontomltomlxmlxmlyamlyamlcsvcsvcborcbor