我想用uiskin.json定义TextButton的背景。

这是我尝试过但没有起作用的方法:

com.badlogic.gdx.scenes.scene2d.ui.Skin$TintedDrawable: {
        img: { file: bg.png }
    },
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
    default: { down: default-round-down, up: img, font: default-font, fontColor: white }
}


所以我想将bg.png作为默认背景。我该怎么做?

最佳答案

皮肤无法从json读取文件位置。如果您遵循有关Skin的教程,则可能使用如下的TextureAtlas实例化了它:

skin = new Skin(skinFilePath, textureAtlas);


像这样加载它时,json中的所有图像都必须通过TextureAtlas中的名称可用。

通常,出于性能考虑,您需要将所有图像都放在一个TextureAtlas中。因此,最好的解决方案是将此bg.png图像添加到TextureAtlas中,然后可以通过其TextureAtlas名称进行引用。

如果必须将其作为单独的文件加载,则必须在加载皮肤之前手动加载它。

Texture bgTexture = new Texture("bg.png");
skin = new Skin(); //empty constructor causes it not to load yet.
skin.add("bg", bgTexture);
skin.addRegions(textureAtlas); // you will be responsible for disposing the atlas separately from the skin now.
skin.load(skinFilePath);

10-08 08:33