mirror of
https://github.com/bevyengine/bevy
synced 2024-11-25 06:00:20 +00:00
6f7d0e5725
# Objective Currently text is recomputed unnecessarily on any changes to its color, which is extremely expensive. ## Solution Split up `TextStyle` into two separate components `TextFont` and `TextColor`. ## Testing I added this system to `many_buttons`: ```rust fn set_text_colors_changed(mut colors: Query<&mut TextColor>) { for mut text_color in colors.iter_mut() { text_color.set_changed(); } } ``` reports ~4fps on main, ~50fps with this PR. ## Migration Guide `TextStyle` has been renamed to `TextFont` and its `color` field has been moved to a separate component named `TextColor` which newtypes `Color`.
83 lines
2.9 KiB
Rust
83 lines
2.9 KiB
Rust
//! Demonstrates how to use transparency with UI.
|
|
//! Shows two colored buttons with transparent text.
|
|
|
|
use bevy::prelude::*;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(ClearColor(Color::BLACK))
|
|
.add_plugins(DefaultPlugins)
|
|
.add_systems(Startup, setup)
|
|
.run();
|
|
}
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
commands.spawn(Camera2d);
|
|
|
|
let font_handle = asset_server.load("fonts/FiraSans-Bold.ttf");
|
|
|
|
commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::SpaceAround,
|
|
..default()
|
|
},
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent
|
|
.spawn(ButtonBundle {
|
|
style: Style {
|
|
width: Val::Px(150.0),
|
|
height: Val::Px(65.0),
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
},
|
|
background_color: Color::srgb(0.1, 0.5, 0.1).into(),
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent.spawn((
|
|
Text::new("Button 1"),
|
|
TextFont {
|
|
font: font_handle.clone(),
|
|
font_size: 33.0,
|
|
..default()
|
|
},
|
|
// Alpha channel of the color controls transparency.
|
|
TextColor(Color::srgba(1.0, 1.0, 1.0, 0.2)),
|
|
));
|
|
});
|
|
|
|
// Button with a different color,
|
|
// to demonstrate the text looks different due to its transparency.
|
|
parent
|
|
.spawn(ButtonBundle {
|
|
style: Style {
|
|
width: Val::Px(150.0),
|
|
height: Val::Px(65.0),
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
..default()
|
|
},
|
|
background_color: Color::srgb(0.5, 0.1, 0.5).into(),
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent.spawn((
|
|
Text::new("Button 2"),
|
|
TextFont {
|
|
font: font_handle.clone(),
|
|
font_size: 33.0,
|
|
..default()
|
|
},
|
|
// Alpha channel of the color controls transparency.
|
|
TextColor(Color::srgba(1.0, 1.0, 1.0, 0.2)),
|
|
));
|
|
});
|
|
});
|
|
}
|