Search code examples
libgdxtexture-packing

LibGdx Texture packing


I want to use animated sprite characters in my LibGdx Project.

I have sprite sheets and also individual images for the sprite animations.

While creating texture atlas,do I have to use sprite sheet itself or individual images for packing? Which one is the best approach?

Currently I'm creating separate atlas for each sprite animations using individual images,and adding the pack& png files to assets folder. It is working. But I hope there is some way to efficiently use one pack file and one png file for the game.

Also I want to know about naming those individual sprite sheet images. I'm naming it in such a way that "name_01,name_02.....etc".

Please suggest the better way of creating single texture atlas for the entire game,that contain sprite sheets and non-sprite images.


Solution

  • Pack all sprite animations images into one pack file.

    Get TextureAtlas from AssetManager if you want to use AssetManager or you can create object of TextureAtlas.

    Then you can call getArray(gameAtlas.findRegion("resourceName"),x,y); method. That returns Array of TextureRegion which can be used as argument of Animation class.

     public TextureRegion[] getArray(TextureRegion textureRegion,int column,int row){
    
            TextureRegion[][] arrayTextureRegion= textureRegion.split(textureRegion.getRegionWidth()/column,textureRegion.getRegionHeight()/row);
            TextureRegion textureRegion1[]=new TextureRegion[arrayTextureRegion[0].length*arrayTextureRegion.length];
    
            for (int i=0;i<arrayTextureRegion.length;i++)
                for (int j=0;j<arrayTextureRegion[i].length;j++)
                    textureRegion1[i*arrayTextureRegion[i].length+j]=arrayTextureRegion[i][j];
    
            return textureRegion1;
        }