英文:
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<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()
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论