Skip to content

Commit 616d0e0

Browse files
committed
feat: example
1 parent 653d061 commit 616d0e0

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,19 @@ description = "Renders text to multiple windows with different scale factors usi
862862
category = "2D Rendering"
863863
wasm = true
864864

865+
866+
[[example]]
867+
name = "system_fonts"
868+
path = "examples/2d/system_fonts.rs"
869+
doc-scrape-examples = true
870+
871+
[package.metadata.example.system_fonts]
872+
name = "System Fonts"
873+
description = "Uses a system font to display text"
874+
category = "2D Rendering"
875+
# Loading asset folders is not supported in Wasm, but required to create the atlas.
876+
wasm = false
877+
865878
[[example]]
866879
name = "texture_atlas"
867880
path = "examples/2d/texture_atlas.rs"

examples/2d/system_fonts.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! Uses a system font to display text
2+
use bevy::prelude::*;
3+
4+
fn main() {
5+
App::new()
6+
.add_plugins(DefaultPlugins)
7+
.add_systems(Startup, setup)
8+
.run();
9+
}
10+
11+
fn setup(mut commands: Commands, mut fonts: ResMut<Assets<Font>>) {
12+
commands.spawn(Camera2d);
13+
14+
let font = fonts.add(Font::System {
15+
families: vec![
16+
Family::Name("Liberation Sans".to_string()),
17+
Family::Name("Ubuntu".to_string()),
18+
Family::Name("Noto Sans".to_string()),
19+
],
20+
weight: Weight::NORMAL,
21+
stretch: Stretch::Normal,
22+
style: Style::Normal,
23+
});
24+
25+
commands.spawn((
26+
Node {
27+
width: percent(100.),
28+
height: percent(100.),
29+
display: Display::Flex,
30+
padding: UiRect::all(px(20.)),
31+
justify_content: JustifyContent::Center,
32+
..default()
33+
},
34+
children![(
35+
Text::new("System Font UI Text"),
36+
TextFont::default().with_font(font.clone()),
37+
)],
38+
));
39+
40+
commands.spawn((
41+
Text2d::new("System Font 2D Text"),
42+
TextFont::default().with_font(font),
43+
));
44+
}

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Example | Description
128128
[Sprite Sheet](../examples/2d/sprite_sheet.rs) | Renders an animated sprite
129129
[Sprite Slice](../examples/2d/sprite_slice.rs) | Showcases slicing sprites into sections that can be scaled independently via the 9-patch technique
130130
[Sprite Tile](../examples/2d/sprite_tile.rs) | Renders a sprite tiled in a grid
131+
[System Fonts](../examples/2d/system_fonts.rs) | Uses a system font to display text
131132
[Text 2D](../examples/2d/text2d.rs) | Generates text in 2D
132133
[Texture Atlas](../examples/2d/texture_atlas.rs) | Generates a texture atlas (sprite sheet) from individual sprites
133134
[Tilemap Chunk](../examples/2d/tilemap_chunk.rs) | Renders a tilemap chunk

0 commit comments

Comments
 (0)