从纹理集中查找区域或创建精灵的纹理

3

我正在尝试从由纹理图集创建的Sprite或Atlas Region中获取纹理。

    atlas=new TextureAtlas(Gdx.files.internal("Test.pack"));
    region=atlas.findRegion("green");

或者
 Sprite s = atlas.createSprite("red");
 texture = s.getTexture();      

我在findRegion和createSprite方法中给出了图像的名称,但它总是选择其他图像对象。我想获取该图像的纹理,以便可以呈现该纹理的一部分。
这是Packer图像: http://i61.tinypic.com/neupo2.png 即使我尝试获取绿色区域或红色区域,它仍然返回蓝色区域。任何帮助将不胜感激。

我已经尝试了所有可能的方法。现在唯一的方法是使用Sprite表格自己编写代码,并读取图像的存储坐标。 - Jatin
4个回答

4

当您执行“textureAtlas.findRegion(“ objectTexture”)。getTexture();”(来自James Skemp的答案)时,您将从图集中获取整个纹理,而不仅仅是“objectTexture”区域的纹理。

不必创建Texture来创建Sprite,您可以使用TextureRegion来创建Sprite。像这样:

atlas=new TextureAtlas(Gdx.files.internal("Test.pack"));
regionGreen=atlas.findRegion("green");
Sprite s = new Sprite(regionGreen);

如何在没有Sprite类的情况下完成这个操作?我只需要区域纹理。 - lacas
在我的例子中,您可以使用“regionGreen”作为TextureRegion。“atlas.findRegion()”返回一个TextureAtlas,它是TextureRegion的子类。 - Ariston Cordeiro

1

我的使用情况略有不同,但我遇到了同样的问题。

我有一个扩展了 Actor 的对象,我想使用图集中的一个区域来进行绘制。

public class MyObject extends Actor {
    private Texture texture;

    public MyObject() {
        TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("xxx.atlas"));
        texture = textureAtlas.findRegion("objectTexture").getTexture();
        Gdx.app.log(TAG, "Texture width: " + texture.getWidth());
        Gdx.app.log(TAG, "Texture height: " + texture.getHeight());

        setBounds(getX(), getY(), Constants.STANDARD_TILE_WIDTH, Constants.STANDARD_TILE_HEIGHT);
        // ...
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(texture,
                worldPosition.x * Constants.STANDARD_TILE_WIDTH, worldPosition.y * Constants.STANDARD_TILE_WIDTH,
                texture.getWidth(), texture.getHeight());
    }
}

我原本只期望得到一个区域,但实际上我收到了整个地图册,因此我的记录纹理的宽度和高度为1024 x 128。

很不幸,我仍然不确定为什么getTexture()返回太多内容,但是转换到使用batchDraw(TextureRegion, ...)至少让我处于一个更好的位置。

public class MyObject extends Actor {
    private TextureRegion texture;

    public MyObject() {
        TextureAtlas textureAtlas = new TextureAtlas(Gdx.files.internal("xxx.atlas"));
        texture = textureAtlas.findRegion("objectTexture");

        setBounds(getX(), getY(), Constants.STANDARD_TILE_WIDTH, Constants.STANDARD_TILE_HEIGHT);
        // ...
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(texture, getX(), getY());
    }
}

根据我在精灵中看到的内容,提问者之所以看到蓝色正方形,原因与我相同;整个图像都是由getTexture()加载的,而且它从左下角开始,你总是会看到一个蓝色正方形。 使用Sprite(TextureRegion)构造函数也可能解决他们的问题。

0

我认为你的问题在于Test.pack文件,而不是纹理。

这个是用GDX Texture Packer打包的工作示例:

Test.png format: RGBA8888 filter: Nearest,Nearest repeat: none blue rotate: false xy: 1, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1 green rotate: false xy: 54, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1 red rotate: false xy: 107, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1 yellow rotate: false xy: 160, 1 size: 51, 51 orig: 51, 51 offset: 0, 0 index: -1


-1

使用索引也可以获取它。从打包文件或 .atlas 文件中获取索引。

atlas=new TextureAtlas(Gdx.files.internal("Test.pack"));
    region=atlas.findRegion("green",index);

或者

 Sprite s = atlas.createSprite("red",index);
 texture = s.getTexture();   

打包文件(.txt)或.pack文件中所有图像的索引都为-1。我需要手动更改文件中的索引,然后再访问它吗?让我也尝试一下这种方法。 - Jatin
我尝试了,但没有成功。还有其他解决方案吗? - Jatin
@Jatin,你只需要在.pack文件中使用索引提到的编号(无论是-1还是其他任何数字)。 - arv
@Jatin 如果解决方案有效,请接受它作为正确答案。 - arv
它没有工作。我现在已经使用软件包中的图像文件编码了。 - Jatin
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接