1
\$\begingroup\$

A similar question was asked before, but I never saw an answer.

How does one load individual texture regions that derive from a texture atlas using assetManager? I know that assetManager relies on a file name. (the file name could be the .pack file, for instance).

How would this work for individual texture regions? What would go in the file handle?

How would I do this:

manager.load("sprite_sheet.txt", TextureAtlas.class);

but with a textureRegion from that atlas?

\$\endgroup\$
0
\$\begingroup\$

You don't, the asset is the TextureAtlas, not the individual TextureRegions.

This means that you let the AssetManager load the TextureAtlas and you manage getting the regions out of it yourself.

One way of doing this is the way Andreas Ohelke does it in Learning libGDX Development where a Assets class load the TextureAtlas and then simple wrapper classes are responsible for getting the regions in an easy to access format:

public class Assets {
    private static final AssetDescriptor<TextureAtlas> SPRITE_ATLAS = new AssetDescriptor<TextureAtlas>("graphics/sprites.atlas", TextureAtlas.class);


    public SpriteAssets sprites;

    public void init(AssetManager assetManager) {
        TextureAtlas spriteAtlas = assetManager.load(SPRITE_ATLAS, TextureAtlas.class);

        assetManager.finishLoading();


        sprites = new SpriteAssets(assetManager.get(SPRITE_ATLAS));

    }

    public class SpriteAssets {

        public final TextureRegion ship;
        public final TextureRegion player;
        public final TextureRegion enemy;

        public SpriteAssets(TextureAtlas atlas) {
            ship = atlas.findRegion("ship");
            player = atlas.findRegion("player");
            enemy = atlas.findRegion("enemy");
        }
    }
}

(full example)

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.