diff --git a/examples/2d_text.rs b/examples/2d_text.rs index 4b2e150..77ac242 100644 --- a/examples/2d_text.rs +++ b/examples/2d_text.rs @@ -1,8 +1,9 @@ use bevy::prelude::*; +use bevy_text_mesh::TextMeshPlugin; fn main() { App::new() - .add_plugins(DefaultPlugins) + .add_plugins((DefaultPlugins, TextMeshPlugin)) .add_systems(Startup, setup) .add_systems(Update, animate_rotation) .run(); diff --git a/examples/3d_scene.rs b/examples/3d_scene.rs index c438a21..903baae 100644 --- a/examples/3d_scene.rs +++ b/examples/3d_scene.rs @@ -13,7 +13,7 @@ fn main() { } fn setup_text_mesh(mut commands: Commands, asset_server: Res) { - let font: Handle = asset_server.load("fonts/FiraMono-Medium.ttf"); + let font: Handle = asset_server.load("fonts/FiraMono-Medium.ttf#mesh"); commands.spawn(TextMeshBundle { text_mesh: TextMesh { diff --git a/examples/performance.rs b/examples/performance.rs index a59c490..ef956b3 100644 --- a/examples/performance.rs +++ b/examples/performance.rs @@ -75,7 +75,7 @@ fn setup_text_mesh( asset_server: Res, ) { let state = SceneState { - font: asset_server.load("fonts/FiraMono-Medium.ttf"), + font: asset_server.load("fonts/FiraMono-Medium.ttf#mesh"), text_count: 0, text_update_count: 0, material: materials.add(StandardMaterial { diff --git a/src/font_loader.rs b/src/font_loader.rs index d7ba904..567b1e4 100644 --- a/src/font_loader.rs +++ b/src/font_loader.rs @@ -1,4 +1,5 @@ use bevy::asset::AsyncReadExt; +use bevy::text::Font; use std::error::Error; use std::fmt::Display; @@ -22,7 +23,7 @@ impl Display for FontLoaderError { pub struct FontLoader; impl AssetLoader for FontLoader { - type Asset = TextMeshFont; + type Asset = Font; type Settings = (); type Error = FontLoaderError; @@ -30,7 +31,7 @@ impl AssetLoader for FontLoader { &'a self, reader: &'a mut Reader, _: &'a Self::Settings, - _: &'a mut LoadContext, + load_context: &'a mut LoadContext, ) -> BoxedFuture<'a, Result> { Box::pin(async move { let mut bytes = Vec::new(); @@ -41,11 +42,15 @@ impl AssetLoader for FontLoader { // ttf fontloading let font = TextMeshFont { - ttf_font: ttf2mesh::TTFFile::from_buffer_vec(bytes) + ttf_font: ttf2mesh::TTFFile::from_buffer_vec(bytes.clone()) .expect("unable to decode asset"), }; - Ok(font) + load_context.add_labeled_asset("mesh".into(), font); + + let original_font = Font::try_from_bytes(bytes.into()).expect("unable to read font"); + + Ok(original_font) }) } diff --git a/src/lib.rs b/src/lib.rs index 4a4d779..6f546a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,10 +26,9 @@ pub struct TextMeshPlugin; impl Plugin for TextMeshPlugin { fn build(&self, app: &mut App) { - app.register_asset_loader(FontLoader) - .init_asset::() + app.init_asset::() .add_systems(Update, (mesh_system::text_mesh, mesh_system::font_loaded)) - .insert_resource(MeshCache::default()) - .init_asset_loader::(); + .init_resource::() + .init_asset_loader::(); } }