Le Duy Khanh
Le Duy Khanh

Reputation: 1369

Libgdx create a TextButton with background with uiskin.json

I want to define the background of TextButton with uiskin.json.

Here is what i tried but didn't work:

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 }
}

So I want to make bg.png as the default background. How can i do it?

Upvotes: 1

Views: 864

Answers (1)

Tenfour04
Tenfour04

Reputation: 93779

Skin cannot read file locations from the json. If you followed tutorials for Skin, you probably instantiated it with a TextureAtlas like this:

skin = new Skin(skinFilePath, textureAtlas);

When you load it like that, all the images in the json must be available by their names in the TextureAtlas.

Normally, you need all your images to be in a single TextureAtlas, for performance reasons. So the best solution would be to add this bg.png image into your TextureAtlas, and then you can refer to it by its TextureAtlas name.

If you must load it as a separate file, then you must manually load it before loading your skin.

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);

Upvotes: 2

Related Questions