【问题标题】:Libgdx create a TextButton with background with uiskin.jsonLibgdx 使用 uiskin.json 创建一个带有背景的 TextButton
【发布时间】:2015-05-10 09:22:17
【问题描述】:

我想用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 作为默认背景。我该怎么做?

【问题讨论】:

    标签: java libgdx scene2d


    【解决方案1】:

    皮肤无法从 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);
    

    【讨论】:

    • 有没有办法在json文件中为不同类型的按钮定义不同的背景?我看到了 BitmapFont 的定义方式,我可以读取字体 png 文件位置。他们的工作方式不一样吗?所以对于这样的事情,我是否需要将所有按钮背景打包到一个纹理然后获取区域?正确的做法是什么?
    • 第一个问题:有一种方法......它在我上面的答案中,但它需要为每个定义的图像文件添加额外的代码。第二个问题:是的,正确的做法是使用 TexturePacker 将所有背景打包到 TextureAtlas 中。 BitmapFont 在 json 中引用的文件是字体文件(通常是.fnt),而不是图像文件。字体文件引用图像。 BitmapFont 可以使用其自己的图像文件或 TextureAtlas 中的图像加载,但是当将其与 Skin 一起使用时,如果您想执行前者,它具有与我上面概述的相同的复杂要求。
    • 你的方法是我目前使用的。我认为应该有类似于 css 的东西,利用 json 文件。无论如何,谢谢。
    • 我认为没有实现支持的原因是如果你不使用图集,性能会很糟糕。
    • 噢,@Tenfour04,你说得对,那样做会很糟糕,让游戏变慢。我现在很清楚。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多