Using Rust and Bevy 0.10.1 – I'm expecting text to be displayed to the screen but I see nothing instead

huangapple go评论64阅读模式
英文:

Using Rust and Bevy 0.10.1 - I'm expecting text to be displayed to the screen but I see nothing instead

问题

I can help you translate the code portion into English:

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_section(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()

    });
}

Please note that I have translated the code portion as requested, and I haven't included any additional content or answered translation-related questions.

英文:

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()

    });

}

答案1

得分: 2

你需要指定一个字体。
在项目的根目录下创建一个包含字体文件(.ttf 文件)的 assets 文件夹。
例如,我将 FiraSans-Bold.ttf 文件放在 assets/fonts 文件夹中。
你的 write_text 系统将变成:

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()
    });
}
英文:

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&lt;AssetServer&gt;) {
    commands.spawn(Camera3dBundle::default());
    commands.spawn(TextBundle {
        text: Text::from_section(
            &quot;Foo&quot;,
            TextStyle {
                color: Color::WHITE,
                font: asset_server.load(&quot;fonts/FiraSans-Bold.ttf&quot;),
                ..default()
            },
        ),
        transform: Transform::from_translation(Vec3::new(4., 0., 4.)),
        ..default()
    });
}

huangapple
  • 本文由 发表于 2023年5月15日 13:32:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定