Add texture to atlas with index

Anything libgdx related goes here!

Add texture to atlas with index

Postby xpdt » Fri Jun 14, 2019 10:24 pm

I need to add some texture to my atlas with index like below:
Code: Select all
textureAtlas.addRegion("texture_0", texture0, 0, 0, texture0.getWidth(), texture0.getHeight());
textureAtlas.addRegion("texture_1", texture1, 0, 0, texture1.getWidth(), texture1.getHeight());
textureAtlas.addRegion("texture_2", texture2, 0, 0, texture2.getWidth(), texture2.getHeight());


I expect to be able to find them with the following code:
Code: Select all
TextureRegion textureRegion = textureAtlas.findRegion("texture", 0);


But i get null.
xpdt
 
Posts: 70
Joined: Mon Jul 04, 2016 2:29 am

Re: Add texture to atlas with index

Postby RafaSKB » Fri Jun 14, 2019 11:08 pm

You have to set the index after adding the region, and also add the region without the index suffixed to the name.

Code: Select all
AtlasRegion atlasRegion0 = textureAtlas.addRegion("texture", texture0, 0, 0, texture0.getWidth(), texture0.getHeight());
atlasRegion0.index = 0;
       
AtlasRegion atlasRegion1 = textureAtlas.addRegion("texture", texture1, 0, 0, texture1.getWidth(), texture1.getHeight());
atlasRegion1.index = 1;
Awesome libGDX - A curated list of resources and tutorials to help developers make awesome games. https://github.com/rafaskb/awesome-libgdx#readme

Grashers - Trash-themed roguelike set in space. http://www.grashers.com
RafaSKB
 
Posts: 141
Joined: Sat Aug 01, 2015 5:14 am
Location: São Paulo, Brazil

Re: Add texture to atlas with index

Postby xpdt » Fri Jun 14, 2019 11:28 pm

Thanks RafaSKB :D

How to remove them from Atlas? Is it possible?
xpdt
 
Posts: 70
Joined: Mon Jul 04, 2016 2:29 am

Re: Add texture to atlas with index

Postby RafaSKB » Sat Jun 15, 2019 1:24 am

I think so yeah, just call "atlas.getRegions()" and remove the region you want from the Array.
Awesome libGDX - A curated list of resources and tutorials to help developers make awesome games. https://github.com/rafaskb/awesome-libgdx#readme

Grashers - Trash-themed roguelike set in space. http://www.grashers.com
RafaSKB
 
Posts: 141
Joined: Sat Aug 01, 2015 5:14 am
Location: São Paulo, Brazil

Re: Add texture to atlas with index

Postby xpdt » Sat Jun 15, 2019 6:10 pm

Thanks, Also need to dispose the texture.
xpdt
 
Posts: 70
Joined: Mon Jul 04, 2016 2:29 am


Return to Libgdx

Who is online

Users browsing this forum: No registered users and 1 guest