1
votes

I am trying to get a texture area from TextureAtlas, but as Texture.

I have tried:

textureAtlas.findRegion("explosion").getTexture();

but it returns the whole texture atlas image.

I need this for a class that I created similar to Sprite. I avoided using Sprite because it couldn't fit my needs.

All answers I found were for the Sprite class.

1

1 Answers

3
votes

You are probably misunderstanding the concept of Texture, TextureRegion and TextureAtlas. There is no simple way to do what you want, nor does it make any sense to do so. Doing so would defeat the whole purpose of using a TextureAtlas in the first place.

You can think of a Texture as a one-on-one mapping with an image file on disk. A .png file for example. So, basically, for every image file on disk that you have, you can have a Texture instance. But you can't have a Texture instance if there isn't an image file on disk from which the texture is loaded. (strictly speaking you could create a Texture instance without file on disk, e.g. an in-memory file so to speak, but that's not relevant for your question)

A TextureAtlas is used to combine multiple smaller images into a single image file on disk. This is done using e.g. TexturePacker, which is a tool that uses multiple images as file on disk as input and then combines it to a single image file on disk. This is done because having one (or a few) big image is alot more performant than having many smaller images.

So, because you are using a TextureAtlas (which is a good thing), this means that you have only one image file on disk. And because you have only one image file on disk you also only have (and need) one Texture instance.

You typically should never use a Texture directly. Instead you should use a TextureRegion. This is simply a class that specifies a Texture along with a specific region (x, y, width and height) within that texture. Which is exactly what you are looking for. You can directly draw a TextureRegion, e.g. using SpriteBatch. So this is a very convenient way to specify an image.

Note that the Sprite class extends TextureRegion and just adds some additional convenient properties. So if you have Sprite then you also have a TextureRegion, you could simply ignore the additional properties if you don't need them.

So, if you created a class that needs an image within a TextureAtlas and relied on a Texture for that, then you implemented that incorrectly. The best you can do in that case is to modify that implementation by using TextureRegion instead of Texture.