Path: blob/main/examples/ui/ui_texture_slice_flip_and_tile.rs
6595 views
//! This example illustrates how to how to flip and tile images with 9-slicing in the UI.12use bevy::{3image::{ImageLoaderSettings, ImageSampler},4prelude::*,5ui::widget::NodeImageMode,6};78fn main() {9App::new()10.add_plugins(DefaultPlugins)11.insert_resource(UiScale(2.))12.add_systems(Startup, setup)13.run();14}1516fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {17let image = asset_server.load_with_settings(18"textures/fantasy_ui_borders/numbered_slices.png",19|settings: &mut ImageLoaderSettings| {20// Need to use nearest filtering to avoid bleeding between the slices with tiling21settings.sampler = ImageSampler::nearest();22},23);2425let slicer = TextureSlicer {26// `numbered_slices.png` is 48 pixels square. `BorderRect::square(16.)` insets the slicing line from each edge by 16 pixels, resulting in nine slices that are each 16 pixels square.27border: BorderRect::all(16.),28// With `SliceScaleMode::Tile` the side and center slices are tiled to fill the side and center sections of the target.29// And with a `stretch_value` of `1.` the tiles will have the same size as the corresponding slices in the source image.30center_scale_mode: SliceScaleMode::Tile { stretch_value: 1. },31sides_scale_mode: SliceScaleMode::Tile { stretch_value: 1. },32..default()33};3435// ui camera36commands.spawn(Camera2d);3738commands39.spawn(Node {40width: percent(100),41height: percent(100),42justify_content: JustifyContent::Center,43align_content: AlignContent::Center,44flex_wrap: FlexWrap::Wrap,45column_gap: px(10),46row_gap: px(10),47..default()48})49.with_children(|parent| {50for [columns, rows] in [[3, 3], [4, 4], [5, 4], [4, 5], [5, 5]] {51for (flip_x, flip_y) in [(false, false), (false, true), (true, false), (true, true)]52{53parent.spawn((54ImageNode {55image: image.clone(),56flip_x,57flip_y,58image_mode: NodeImageMode::Sliced(slicer.clone()),59..default()60},61Node {62width: px(16 * columns),63height: px(16 * rows),64..default()65},66));67}68}69});70}717273