Skip to content

Using sprite atlas with the UI #1169

Closed
Closed
@shosanna

Description

@shosanna

What problem does this solve or what need does it fill?

I would like to be able to use something like SpriteSheetBundle but for the UI. I have my UI sprites in an atlas and it would be convenient to be able to use it and not have to export the individual images with ImageBundle

Describe the solution would you like?

To be able to use SpriteSheetBundle as a UI element - be able to style it and position it, etc.

Describe the alternative(s) you've considered?
Additional context

I did not know it is not possible and I was actually trying to do something like this:

    let player_texture_handle = asset_server.load("heart.png");
    let texture_atlas = TextureAtlas::from_grid(player_texture_handle, Vec2::new(16.0, 16.0), 2, 1);
    let texture_atlas_handle = texture_atlases.add(texture_atlas);

    commands.spawn(NodeBundle {
        style: Style {
            position_type: PositionType::Absolute,
            justify_content: JustifyContent::Center,
            align_items: AlignItems::FlexEnd,
            ..Default::default()
        },
        ..Default::default()
    })
    .with_children(|parent| {
        parent.spawn(SpriteSheetBundle {
            texture_atlas: texture_atlas_handle,
            transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
            ..Default::default()
        });
    });

And it did not work. I understand it is because I am mixing UI and non-UI elements but I think the error message should be more helpful, I was really struggling to understand what is wrong. The error I got:

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value'

So I think a bit nicer error message would be also welcome for every other beginner out there like I am :)

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-UIGraphical user interfaces, styles, layouts, and widgetsC-FeatureA new feature, making something new possible

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions