libGDX JSON File Reading Error: Unable to Load Skin Resources

18 Views Asked by At

Im trying to add a Slider into my settings menu and it´s not working at all.

Im initializing my Slider like this, in my Game class:

    public Skin sliderSkin;
    public void create() {
        sliderSkin = new Skin(Gdx.files.internal("assets/skins/slider-skin.json"));

The json looks like this (I have validated it)

{
  "com.badlogic.gdx.scenes.scene2d.ui.Slider$SliderStyle": {
    "default-horizontal": {
      "background": "sliderBackground"
    }
  }
}

The file is located in:

- Project
-- assets
--- skins
---- slider-skin.json

I also have a sliderBackground.png located in the same folder as the json file.

I´m also getting this error: Caused by: com.badlogic.gdx.utils.GdxRuntimeException: No Drawable, NinePatch, TextureRegion, Texture, or Sprite registered with name: sliderBackground

Then, im getting this error while running the app:

Caused by: com.badlogic.gdx.utils.SerializationException: Error reading file: assets/skins/slider-skin.json

Ive tried changing the names of the files, the paths, the way Im initializing the Slider, and other things. I just want a slider to show, preferrably with my custom skin.

1

There are 1 best solutions below

0
bornander On

A Skin expects to find the referenced images as TextureRegions in a TextureAtlas, in your case I cannot see how you are loading the sliderBackground.png into an TextureAtlas.

You need to either have an atlas file called slider-skin.atlas in the same folder as your slider-skin.json (which contains a region called sliderBackground that has the image), or you need to manually load the atlas and pass it as the second argument to the Skin constructor.

The libGDX tool Texture Packer can be used to create the TextureAtlas, this is probably the most convenient way.

Or, if you want to just make it work for this one case you could try the following:

TextureAtlas atlas = new TextureAtlas();
atlas.addRegion("sliderBackground", new TextureRegion(new Texture(Gdx.files.internal("assets/skins/sliderBackground.png"))));
sliderSkin = new Skin(Gdx.files.internal("assets/skins/slider-skin.json", atlas));

But I would strongly recommend looking into the texture-packer and using that instead.