2

I'm attempting to write the text "Foo" to a blank window using Rust and bevy = 0.10.1. As of version 0.10, the way to update text for a spawned entity is by using the text: Text::from_selection(value, style) submitted to TextBundle as noted here: https://docs.rs/bevy/latest/bevy/prelude/struct.TextBundle.html. However, nothing is ever drawn to the screen.

use bevy::math::Vec3;
use bevy::prelude::*;

fn main() {
  App::new()
    .add_plugins(DefaultPlugins)
    .add_startup_system(write_text)
    .run();
}

fn write_text(mut commands: Commands,) {     
    commands.spawn(Camera3dBundle::default());

    commands.spawn( TextBundle {
        
        text: Text::from_section("Foo", TextStyle {
        color: Color::WHITE,
        ..default()
        }),
        transform: Transform::from_translation(Vec3::new(4., 0., 4.)),
        ..default()

    });

}
juju
  • 884
  • 1
  • 9
  • 31
  • I am fairly certain you can only use a `TextBundle` with a 2dCamera. However you can use something like [`meshtext`](https://github.com/FrankenApps/meshtext) as demonstrated [`here`](https://github.com/FrankenApps/bevy_meshtext_sample/tree/master). – frankenapps May 15 '23 at 18:40

1 Answers1

2

You need to specify a font. Create an assets folder containing a font (.ttf file) at the root of your project. For instance I put the FiraSans-Bold.ttf file in assets/fonts. Your write_text system becomes:

fn write_text(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(Camera3dBundle::default());
    commands.spawn(TextBundle {
        text: Text::from_section(
            "Foo",
            TextStyle {
                color: Color::WHITE,
                font: asset_server.load("fonts/FiraSans-Bold.ttf"),
                ..default()
            },
        ),
        transform: Transform::from_translation(Vec3::new(4., 0., 4.)),
        ..default()
    });
}
rochard4u
  • 629
  • 3
  • 17
  • 1
    Bevy 0.11 now offers a [default font](https://bevyengine.org/news/bevy-0-11/#default-font) which was implemented [in this PR](https://github.com/bevyengine/bevy/pull/8445). – frankenapps Jul 10 '23 at 16:20