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

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

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:

  1. 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.
  2. use bevy::math::Vec3;
  3. use bevy::prelude::*;
  4. fn main() {
  5. App::new()
  6. .add_plugins(DefaultPlugins)
  7. .add_startup_system(write_text)
  8. .run();
  9. }
  10. fn write_text(mut commands: Commands,) {
  11. commands.spawn(Camera3dBundle::default());
  12. commands.spawn( TextBundle {
  13. text: Text::from_section("Foo", TextStyle {
  14. color: Color::WHITE,
  15. ..default()
  16. }),
  17. transform: Transform::from_translation(Vec3::new(4., 0., 4.)),
  18. ..default()
  19. });
  20. }

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.

  1. use bevy::math::Vec3;
  2. use bevy::prelude::*;
  3. fn main() {
  4. App::new()
  5. .add_plugins(DefaultPlugins)
  6. .add_startup_system(write_text)
  7. .run();
  8. }
  9. fn write_text(mut commands: Commands,) {
  10. commands.spawn(Camera3dBundle::default());
  11. commands.spawn( TextBundle {
  12. text: Text::from_section("Foo", TextStyle {
  13. color: Color::WHITE,
  14. ..default()
  15. }),
  16. transform: Transform::from_translation(Vec3::new(4., 0., 4.)),
  17. ..default()
  18. });
  19. }

答案1

得分: 2

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

  1. fn write_text(mut commands: Commands, asset_server: Res<AssetServer>) {
  2. commands.spawn(Camera3dBundle::default());
  3. commands.spawn(TextBundle {
  4. text: Text::from_section(
  5. "Foo",
  6. TextStyle {
  7. color: Color::WHITE,
  8. font: asset_server.load("fonts/FiraSans-Bold.ttf"),
  9. ..default()
  10. },
  11. ),
  12. transform: Transform::from_translation(Vec3::new(4., 0., 4.)),
  13. ..default()
  14. });
  15. }
英文:

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:

  1. fn write_text(mut commands: Commands, asset_server: Res&lt;AssetServer&gt;) {
  2. commands.spawn(Camera3dBundle::default());
  3. commands.spawn(TextBundle {
  4. text: Text::from_section(
  5. &quot;Foo&quot;,
  6. TextStyle {
  7. color: Color::WHITE,
  8. font: asset_server.load(&quot;fonts/FiraSans-Bold.ttf&quot;),
  9. ..default()
  10. },
  11. ),
  12. transform: Transform::from_translation(Vec3::new(4., 0., 4.)),
  13. ..default()
  14. });
  15. }

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:

确定