Changing the Background Color
Relevant official examples:
clear_color
.
Click here for the full example code.
Use the ClearColor
resource to choose the
default background color. This color will be used as the default for all
cameras, unless overriden.
Note that the window will be black if no cameras exist. You must spawn at least one camera.
fn setup(
mut commands: Commands,
) {
commands.spawn(Camera2dBundle::default());
}
fn main() {
App::new()
// set the global default
.insert_resource(ClearColor(Color::rgb(0.9, 0.3, 0.6)))
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.run();
}
To override the default and use a different color for a specific
camera, you can set it using the Camera2d
or
Camera3d
components.
use bevy::core_pipeline::clear_color::ClearColorConfig;
fn setup_camera_2d(
mut commands: Commands,
) {
// set the color for a specific camera (2D)
commands.spawn(Camera2dBundle {
camera_2d: Camera2d {
clear_color: ClearColorConfig::Custom(Color::rgb(0.8, 0.4, 0.2)),
},
..Default::default()
});
}
fn setup_camera_3d(
mut commands: Commands,
) {
// set the color for a specific camera (3D)
commands.spawn(Camera3dBundle {
camera_3d: Camera3d {
clear_color: ClearColorConfig::Custom(Color::rgb(0.8, 0.4, 0.2)),
..Default::default()
},
..Default::default()
});
}
All of these locations (the components on specific cameras, the global default resource) can be mutated at runtime, and bevy will use your new color. Changing the default color using the resource will apply the new color to all existing cameras that do not specify a custom color, not just newly-spawned cameras.